php and mysql are a golden combination. Now let’s talk about the implementation of querying mysql database records in php. The four php functions mysql_connect mysql_query mysql_select_db mysql_fetch_array are mainly used. Let’s look at the implementation below.
PHP tutorials and mysql tutorials are a golden combination. Now let’s talk about the implementation of PHP query mysql database tutorial records. The four PHP functions mysql_connect mysql_query mysql_select_db mysql_fetch_array are mainly used. Let’s look at the implementation below.
*/
$hostname="localhost"; //Define the mysql server name to connect to
$username="root"; //Define the username used to connect
$password=""; //Define the password used to connect
$link=mysql_connect($hostname,$username,$password); //Open msql connection
mysql_query('set names gb2312;'); //Convert encoding to support Chinese
mysql_select_db("test"); //Select the operation library as test
$query="select * from friends"; //Define sql
$result=mysql_query($query); //Send sql query
while($rows=mysql_fetch_array($result))
{
echo "id is:".$rows[id];
echo "
www.bKjia.c0mn";
echo "name is:".$rows[name];
echo "
n";
echo "sex is:".$rows[sex];
echo "
n";
echo "birthday is:".$rows[birthday];
echo "
n";
echo "address is:".$rows[address];
echo "n";
}
//Release the result set
mysql_free_result($result);
//Conditional query
$query="select * from friends where id=1"; //Define sql
$result= mysql_query($query); //Send sql query
echo mysql_result($result,0,"name"); //Output name result
echo "
";
echo mysql_result($result,0,"birthday"); //Output birthday result
echo "
";
echo mysql_result($result,0,"sex"); //Output sex result
echo "
";
echo mysql_result($result,0,"address"); //Output address result
//Related operations
$conn=mysql_connect('localhost','root',''); //Open the connection
$fields=mysql_list_fields("test","friends",$conn); //List the information of the friends table in the test library
$cols=mysql_num_fields($fields); //Get the number of results
for($i=0;$i<$cols;$i++) //Loop
{
echo mysql_field_name($fields,$i)."n"; //Output field name
echo "";
}
/*
Summary
Querying database records in php is quite simple and commonly used. As long as you record the above functions, you can implement data query.
*/