Php code for querying mysql data table records. Php and mysql are prime combinations. now let's talk about the implementation of querying mysql database records in php. The four php and mysql functions mysql_connectmysql_querymysql_select_dbmysql_fetch_array are prime combinations, now let's take a look at the implementation of php query mysql database records, mainly using the four php functions mysql_connect mysql_query mysql_select_db mysql_fetch_array. The following describes the implementation.
The php Tutorial and mysql tutorial are a golden combination. now let's talk about the implementation of the php query mysql database tutorial record, mainly using the function mysql_connect mysql_query mysql_select_db mysql_fetch_array. The following describes the implementation.
*/
$ Hostname = "localhost"; // defines the name of the connected mysql server
$ Username = "root"; // defines the user name used for connection.
$ Password = ""; // defines the password used for connection.
$ Link = mysql_connect ($ hostname, $ username, $ password); // open the msql connection
Mysql_query ('set names gb2312; '); // Convert the encoding to support Chinese characters
Mysql_select_db ("test"); // select test as the operation Library
$ Query = "select * from friends"; // defines the SQL
$ Result = mysql_query ($ query); // send an SQL query
While ($ rows = mysql_fetch_array ($ result ))
{
Echo "id:". $ rows [id];
Echo"
Www. bKjia. c0mn ";
Echo "name:". $ rows [name];
Echo"
N ";
Echo "sex:". $ rows [sex];
Echo"
N ";
Echo "birthday:". $ rows [birthday];
Echo"
N ";
Echo "address:". $ rows [address];
Echo"
N ";
}
// Release the result set
Mysql_free_result ($ result );
// Conditional query
$ Query = "select * from friends where id = 1"; // define an SQL
$ Result = mysql_query ($ query); // send an SQL query
Echo mysql_result ($ result, 0, "name"); // output the name result
Echo"
";
Echo mysql_result ($ result, 0, "birthday"); // output the birthday result
Echo"
";
Echo mysql_result ($ result, 0, "sex"); // output the sex result
Echo"
";
Echo mysql_result ($ result, 0, "address"); // output the address result
// Related operations
$ Conn = mysql_connect ('localhost', 'root', ''); // open the connection
$ Fields = mysql_list_fields ("test", "friends", $ conn); // lists information about the test database friends table.
$ Cols = mysql_num_fields ($ fields); // Obtain the number of results
For ($ I = 0; $ I <$ cols; $ I ++) // Loop
{
Echo mysql_field_name ($ fields, $ I). "n"; // output field name
Echo"
";
}
/*
Summary
Query Database records in php is quite simple and commonly used. you can query data as long as you record the above functions.
*/
Define mysql_query mysql_select_db mysql_fetch_array four...