PHP mysql_connect and Mysql_pconnect implementation and detailed description

Source: Internet
Author: User

$hostname = "localhost"; To define the MySQL server name to connect to
$username = "root"; Define the user name to use for the connection
$password = ""; Define the password to use for the connection
$link =mysql_connect ($hostname, $username, $password); Connecting to a local MySQL server
if ($link)//if successfully connected
{
echo "Successful connection"; Output content
}
else//If the connection fails
{
echo "Connection Failed"; Output content
}
Mysql_close ($link)//Close the MySQL connection that has been opened

Mysql_pconnect Connection MySQL Database

$link =mysql_pconnect ($hostname, $username, $password); Open a persistent connection
if (! $link)//if unable to connect
{
Die (' Cannot connect '. Mysql_error ()); Output information
Exit (); End All PHP operations
}
echo ' Continuous connection success ';

/*
Below look at the soft connection query database content

*/

$link =mysql_connect ($hostname, $username, $password) or die ("Could not connect:". Mysql_error ());
Convert encoding to support Chinese
mysql_query (' Set names gb2312; ');
Select Action Library Test
mysql_select_db ("test") or die ("could not select Database:". mysql_error ());
Execute SQL query, select name from table
$query = "Select name from Friends";
$result =mysql_query ($query) or Die ("Query failed:". Mysql_error ());
Match result set to row loop output
For ($i =mysql_num_rows ($result)-1; $i >=0; $i--)
{
A pointer that moves the internal result, and if there is no result, the content is lost
if (!mysql_data_seek ($result, $i))
{
echo "Cannot 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 result Content
echo "$row->name<br/>n";
}
Releasing the result set
Mysql_free_result ($result);

/*
Other actions

The

$escaped _item=mysql_escape_string ($STR);     //escapes the string
printf ("escaped String:%sn ", $escaped _item),     //output-escaped results

$mydb =mysql_list_dbs ($link);       //lists the database
while ($MYDB))    //loops through the result set and assigns values to the object
{
  echo $result->database. " n ';       //output object content
  echo <br>;
}

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.