php mysql_connect and mysql_pconnect achieve with a detailed description

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags close connect connected content continue continuous difference echo

In the php tutorial to connect mysql tutorial Database tutorial There are two if there are mysql_connect and mysql_pconnect, one is a soft connection, one is continuous connection, the difference between the two is that when the page browsing automatically closes the mysql connection page has been opened mysql_pconnect If you do not close Always connected.
* /

$ hostname = "localhost"; // Define the name of the mysql server to connect to
$ username = "root"; / / define the user name for the connection
$ password = ""; / / Define the password for the connection
$ link = mysql_connect ($ hostname, $ username, $ password); // connect to the local mysql server
if ($ link) // if successful connection
{
echo "successfully connected"; / / Output
}
else // if the connection failed
{
echo "Connection failed"; // output content
}
// mysql_close ($ link) / / Close the mysql connection has been opened

// mysql_pconnect connect mysql database

$ link = mysql_pconnect ($ hostname, $ username, $ password); // Open the persistent connection
if (! $ link) // if it can not connect
{
die ('can not connect' .mysql_error ()); // output information
exit (); // End all php operations
}
echo 'continue to connect successfully';
/ *
Below to see the soft link to query the contents of the database

* /

$ link = mysql_connect ($ hostname, $ username, $ password) or die ("could not connect:". mysql_error ());
// Convert encoding to support Chinese
mysql_query ('set names gb2312;');
// Select the operation library test
mysql_select_db ("test") or die ("could not select database:". mysql_error ());
// Execute sql query, select the name from the table
$ query = "select name from friends";
$ result = mysql_query ($ query) or die ("query failed:". mysql_error ());
// Matches the result set to the line loop output
for ($ i = mysql_num_rows ($ result) -1; $ i> = 0; $ i--)
{
// Move the pointer to the internal result, if there is no result then the content is lost
if (! mysql_data_seek ($ result, $ i))
{
echo "can not seek to row $ i:". mysql_error (). "n";
continue
}
// Get a row from the query result as an object
if (! ($ row = mysql_fetch_object ($ result)))
continue
// output the content of the result
echo "$ row-> name n";
}
// Release the result set
mysql_free_result ($ result);
/ *
Other operations

$ escaped_item = mysql_escape_string ($ str); // Escape the string
printf ("escaped string:% sn", $ escaped_item); // Output the result of the escape

$ mydb = mysql_list_dbs ($ link); // List the database
while ($ result = mysql_fetch_object ($ mydb)) // loop through the result set and assign to the object
{
echo $ result-> database. "n"; / / Output object content
echo "<br>";
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.