PHP connection MySQL Database

Source: Internet
Author: User
Tags php and mysql

today learn to connect MySQL database with PHP, write a blog summary.
To use PHP to connect to the MySQL database, we will first connect to the database, we use the following function to connect
$sql _link = mysql_connect ("hostname", "Login username", "Login user name password");
If the connection is successful, a MySQL handle is returned, which can be easily understood as the $sql _link is a bridge between PHP and MySQL, and we can get to MySQL through this bridge.
After entering MySQL, we will select a database to operate on MySQL, we use the following function to select a database
mysql_select_db ("Database name", "This parameter can not");
then we execute some MySQL statements, using the following function
$result = mysql_query ("SQL statement", "This parameter can not be");
when you execute some MySQL statements, there are two things:
1. No results are returned, such as deleting data, modifying the encoding, etc.
2. There are return results, such as querying the data, at which point we use a variable to receive. For example, the following statement, which returns a table, can be seen as a two-dimensional array.
$result = mysql_query ("SELECT * from Person_list", $sql _link);//Returns a resource, currently a table
by $result can know a lot of information about this table, we need to use the corresponding function to get the information we need.
1. This table has several fields.
Mysql_num_fields ($result);
2. The table has several records.
Mysql_num_rows ($result);
3. The field name of this table.
Mysql_field_name ($result, $index);
4. In an indexed array, get the array by one line, and perform a pointer offset once.
Mysql_fetch_row ($result);
5. In the form of an associative array, one-by-one gets the array and executes the pointer offset once.
MYSQL_FETCH_ASSOC ($result);
6. Specify the position of the pointer yourself.
Mysql_data_seek ($result, 3);
7. Sometimes we manipulate the database to determine if there is success by the number of rows affected.
Mysql_affected_rows ();//can have parameters
here is a complete code to demonstrate reading data (the Person_list table under the test database) and displaying it on the page (and not dealing with garbled pages).
<?phpheader (' content-type:text/html; Charset=utf-8 ');//information for the database to be connected define ("Db_host", "localhost");//hostname define (" Db_user "," root ");//Login name define (" Db_user_pwd "," rootmysql ");//login user password//connect to database $sql_link = mysql_connect (db_host,db_ USER,DB_USER_PWD), if ($sql _link)//To determine if the connection is successful echo "Connection database successfully <br/>"; Elseecho mysql_error ($sql _link). " <br/> "; $db _name =" Test ";//which database mysql_select_db to connect to ($db _name, $sql _link); $sql _sentence =" SELECT * FROM Person_ List ";//The SQL statement to execute//$sql _sentence =" desc person_list ";//The SQL statement to execute $result = mysql_query ($sql _sentence, $sql _link);// Returns a resource that is currently a table echo ' <table border= ' 1px "width=" 500px "height=" 400px "> '; Echo ' <tr> '; for ($index = 0; $index & Lt Mysql_num_fields ($result); $index + +) echo ' <td> ' Mysql_field_name ($result, $index). '  </td> '; Echo ' </tr> '; while ($row = Mysql_fetch_row ($result)) {echo ' <tr> '; foreach ($row as $value) echo ' <td> '. $value. ' </td> ';//echo ' <td> '. $value. '  </td> '; Echo ' </tr> ';}Echo ' </table> ';? >

PHP connection MySQL Database

Related Article

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.