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