PHP Note 08: Database Programming---Using PHP's MySQL extension library to operate the MySQL database

Source: Internet
Author: User

1. Use the MySQL extension Library of PHP to operate the MySQL database:

PHP has 3 ways to operate MySQL database (1) MySQL extension library (2) mysqli extension Library (3) PDO   MySQL extension library is different from MySQL database?
PHP Designers encapsulate a number of ways to operate the MySQL database, and these methods are centralized to form the MySQL extension library. The MySQL database holds the data.     2. Use PHP's MySQL Extension library operation MySQL database case: (1) Environment construction: Enable MySQL database, configure the use of MySQL database in the php.ini file, Extension = Php_mysql.dll can be obtained by: in http://localhost/test.php, here in the test.php text to write: <?php phpinfo ();// output PHP can use the extension library, verify whether the MySQL service is turned on ?>   (2) Create a user table for us to use: CREATE TABLE User1 ( ID int primary key auto_increment, name varchar (+) is not NULL, passwd varchar (+) NOT NULL, Email varchar (+) NOT NULL, Age tinyint unsigned Not null ) ;   Pre-join data: insert INTO User1 (NAME,PASSWD, Email,age) VALUES (' Zs ', MD5 (' 123456 '), ' [email protected] ', (+); insert INTO User1 (NAME,PASSWD, Email,age) VALUES (' ls ', MD5 (' 123456 '), ' [email protected] ', +);
insert INTO User1 (NAME,PASSWD, Email,age) VALUES (' WW ', MD5 (' 123456 '), ' [email protected] ', [e]];
insert INTO User1 (NAME,PASSWD, Email,age) VALUES (' Judges ', MD5 (' 123456 '), ' [email protected] ', [e]];
Insert Chinese here will error: The following settings are required: Show variables like '%char% '; set character_set_client = GBK; set character_set_client = GBK; (2) write the PHP program, complete the display of the user table: steps:-->1: Get connection: Get the MySQL extension library and MySQL database connection--&GT;2: Select Database-->3: set encoding (recommended)-->4: Send instruction SQL-->5: receives the returned result and processes it. (show)-->6: freeing resources, closing connections
<?PHPHeader("content-type:text/html; Charset=utf-8 "); //MySQL extension library operations MySQL database steps are as follows://1. Get connection: Get the MySQL extension library and MySQL database connection   $conn=mysql_connect("127.0.0.1", "root", "root");//parameter 1: Host name, Parameter 2: User name, Parameter 3: password;   if(!$conn) {        die("Connection failed.")Mysql_error()); }   //2. Select a database   mysql_select_db("Test"); //3. Set the encoding (recommended)//mysql_//4. Send instruction SQL (DDL data definition Statement), DML (Data manipulation statement Update insert delete), DQL (data Query Language Select), DTL (Data transaction language RO Llback commit ... )   $sql= "Select *from user1"; //The function//$res represents the result set, which you can simply understand is a table   $res=mysql_query($sql,$conn); //Var_dump ($res); 5. Receive the returned results and process them. (display)//mysql_fetch_row the next row of data in the $res result set, assigned to $row//$row is an array    while($row=Mysql_fetch_row($res)){       //the first: echo "<br/> $row [0]--$row [1]--$row [2];       The second kind: echo "<br/>";       Var_dump ($row); The third type:       foreach($row  as $key=$val) {           Echo"--$val"; }       Echo"<br/>"; }   //6. Release the resource, close the connection     Mysql_free_result($res); Mysql_close($conn); ?>

The above code mentions three ways to display the data as follows:

The first type: echo "<br/> $row [0]--$row [1]--$row [2]";

The second type: echo "<br/>"; var_dump ($row);

The third type: foreach ($row as $key = = $val) { echo "-$val";                 } echo "<br/>";

Mysql_free_result ($res); frees up computer resources , or the computer gets stuck!

Mysql_close ($conn);// Release the connection , if there is no such sentence, we repeatedly refresh the page access .../mysql/mysqldemo1.php, there will be a lot of time_wait Connection request: The following

After repeatedly refreshing the page, we enter the cmd command:


View results:



PHP Note 08: Database Programming---Using PHP's MySQL extension library to operate the MySQL database

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.