Php code for querying mysql data table records _ PHP Tutorial

Source: Internet
Author: User
Tags mysql functions
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...

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.