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>";
}