Learn php-(16) PHP operation database using MySQL extension library

Source: Internet
Author: User
Tags learn php

PHP provides a number of extension libraries, which is said to use the MySQL extension library, but this extension library will be discarded in the near future, because if the code written using the MySQL extension library will be warning when running. I wanted to write the other one directly, but I felt it was the foundation. MySQL extension library, when it comes to libraries, it's natural to think of a bunch of functions, a lot of functions that make up a library, and the use of an extension library is the function inside. The MySQL extension library is completely process-oriented and clearly incompatible with object-oriented features, and is also understandable to be discarded. Nonsense not much to say, directly on the point.

PHP uses the MySQL extension library to operate the databaseThis picture is my own painting, there may be inaccuracies in the place. you can see that using the MySQL operations database is roughly five steps:1. Connect to MySQL serverThe function used here is the mysql_connect () function2. Select Databasethe mysql_select_db () is used here;3. Execute SQL statementsthis uses the mysql_query () function. The so-called SQL statement is to delete and change the operation. 4. Close result setthis uses the Mysql_free_result () function to release system resources.5. Disconnecting from the serverhere use mysql_close (); functionSecond, elaborate five step 1, connect MySQL serverThe syntax for the mysql_connect () function is as follows: Resource mysql_connect ([string server[,string username[,string password[,bool new_link[,int client_ Flags]]);This is the most complete function parameter, but we only use the first three parameters when we use it. The first server refers to the MySQL server, which can use the default localhost. The second username is the user name that you set when you install the database, and we default to root. A third password is the password for the database. If the connection succeeds, it returns a MySQL connection ID, and the failure returns false. For example, I connect my own database, my server is localhost, the user name is root, the password is root. I can test this .
<?php$host = ' localhost '; $username = ' root '; $password = ' root '; if ($conn = mysql_connect ($host, $username, $password)) {echo "connection to MySQL server succeeded";} Else{echo "Connection Failed";}

2. Select Databasebool mysql_select_db (string Database_name[,resource link_identifier]); the first parameter, database_name, is the name of the database to connect to. the second parameter, Link_identifier, is the database connection ID, which is the value returned when the MySQL server above connects successfully. If you do not write, the default is the last Open database connection. Suggested write. The success returns True, and the failure returns false. For example, I need to connect to the test database I created last time, so I can write:
<?php$host = ' localhost '; $username = ' root '; $password = ' root '; $database _name = ' Test '; $conn = mysql_connect ($host, $ Username, $password), if (mysql_select_db ($database _name, $conn)) {echo "database connection succeeded";} Else{echo "Connection Failed";}


3. Execute SQL statementsThe so-called SQL statement is the database to make additions and deletions to the operation. This is already described when using the command line to manipulate the database. The function syntax is as follows:resource mysql_query (string Query[,resource link_identifier]); The first parameter is the SQL statement that needs to be executed. The second parameter is the connection identity returned by Mysql_connect. Suggested write. SQL statements are divided into DDL "data definition statements", DML "data manipulation statements, such as Update,insert,delete", dql "select", DTL "Data transaction statement rollback commit", The most commonly used in this area is the DQL statement, which is the query statement. After the execution of the DQL statement succeeds, a "data pointer" is returned, which returns true after the execution of the DML statement, and the failure returns false. For example, I want to find out what data I have in my table, so I can write it.
<span style= "FONT-SIZE:14PX;" ><?php$host = ' localhost '; $username = ' root '; $password = ' root '; $database _name = ' Test '; $conn = mysql_connect ($ Host, $username, $password); mysql_select_db ($database _name, $conn); $sql = "select * from Tb_student;"; $query = mysql_query ($sql, $conn), while ($res = Mysql_fetch_assoc ($query)) {echo $res [' id ']. ' --'. $res [' name ']. ' --'. $res [' email ']. ' --'. $res [' address ']. ' --'. $res [' age ']. ' <br> ';} </span>

Additions and deletions to change the example, is to change the SQL statement. Then returns the true or FALSE. No more result sets are taken. 4. Close result setMysql_free_result (Resource $res);For example, I can close the result set after I use it, mysql_free_result ($query); 5. Disconnect from DatabaseMysql_close (Resource $link _identifier);For example, the above program finally I can disconnect, mysql_close ($conn); three, here is a comprehensive case
<?php//mysql Expansion Library Operations database steps are as follows//1. Get Connection $conn = mysql_connect ("localhost", "root", "root");     if (! $conn) {die ("Connection failed". Mysql_error);     }//2. Select Database mysql_select_db ("Mr_mysql");     3. Set the Operation code (recommended) mysql_query ("Set names UTF8");  4. Send instruction SQL (DDL "data definition statement", DML "data action statement, such as Update,insert,delete", dql "select", DTL "Data transaction statement rollback commit") $sql = "Select     * from Mr_user; ";     $res = mysql_query ($sql, $conn);//The Returned data ' pointer ' does not refer to the first row, but to the first row of the line, which is the No. 0 row.         Var_dump ($res);           5. Receive the returned result and process the while ($row = Mysql_fetch_row ($res)) {//mysql_fetch_row () is the next row of the result set and assigns a value to $row//$row is an array          The first type of extraction: $row [i] echo "<br>";          echo "$row [0]--$row [1]--$row [2]--$row [3]--$row [4]";          Var_dump ($row);          The second type of foreach ($row as $key = + $value) {echo "$value-";     }}//6. Release resources mysql_free_result ($res);  Mysql_close ($conn); This sentence can not write, suggest write

Iv. details 1. When you are finished using the $res result set, be sure to release the resource
2.mysql_close () If not, the system will turn off automatically.
3. If Mysql_close is placed in front of the while, it will not affect the result, because after executing the mysql_query statement, the program has read the database to memory and is read from memory when used again. But if Mysql_free_result is in front, then it will go wrong.
4. $row is a number of groups
5. $res If you are performing a DQL statement that returns a resource type, but if you are executing a DML statement, you will return a bool value of 6. There are three methods in addition to Mysql_fetch_row ($res) [return index array] when fetching row data from $res: MYSQL_FETCH_ASSOC ($res)--------returns an associative array mysql_fetch_array ($res)---------returns an indexed array and an associative array , consumes memory, it is recommended not to use Mysql_fetch_object ($res)---------to return a row of data as an object

This is basically done using the MySQL extension Library operations database, which is the basis for the subsequent use of the MYSQLI extension Library operations database, and the next write mysqli the extended library operations database.











Learn php-(16) PHP operation database using MySQL extension library

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.