Home >Backend Development >PHP Tutorial >PHP introductory learning knowledge point 1 PHP and MYSql connection and query_PHP tutorial
1. Connect to the database
1. mysql_connect: Open the MySQL link
mysql_connect('host', 'username', 'password')
2. mysql_select_db : Open a database
mysql_select_db('database name',$link identifier) //If the link identifier is not filled in, it will default to the last opened connection
3. mysql_query("set names 'GBK' ") Solve the problem of Chinese garbled characters;
mysql_query("set names 'Encoding (utf8 or GBK)' ") //UTF8 cannot have "-"
2. Query data
1. mysql_query (SQL statement, connection identifier);
$sql="Select * FROM Test "
$result=mysql_query($sql) //Connection identifier The symbol defaults to the last opened link
//Get error information
$result=@mysql_query($sql) or die(mysql_error())
2. Get Query results
a. mysql_fetch_row($result);
$row=mysql_fetch_row($result);
echo $row[0];
b , mysql_fetch_array($result);
$row=mysql_fetch_array($result);
echo $row[0];
echo $row['key'];
Note: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that in addition to using offsets starting from 0 as index, it can also use domain names as index.
The value returns all the field values of the next row and saves them in an array. If there is no row, it returns false.