PHP database programming---mysql extension library

Source: Internet
Author: User
Tags php database

1, Java has a way to operate the database, PHP has three ways to operate the MySQL database. (1) MySQL extension library; (2) Mysqli extension Library; (3) PDO;

2, MySQL extension library and MySQL database differences

3, three-layer structure of MySQL database

4, MySQL extension library is a bunch of functions that PHP designers provide to programmers to complete various operations (CRUD) on the MySQL database. Use PHP's MySQL extension library to complete the case of MySQL operations: write a program that reads data from the User1 table and prints it in a Web page.

(1) Environment construction

① enable MySQL extension library, configure the MySQL extension library in the php.ini file, Extension=php_mysql.dll. You can <?php phpinfo ()?> See what extension libraries are currently supported by PHP.

② Create a user table that inserts data into the table for use by the program.

(2) write the PHP program, complete the display of the user table.

$conn = mysql_connect ("10.3.255.21", "Liuhuayong", "AWMY6HQLF", "3400"), if (! $conn) {die    ("Connection Failed". Mysql_error ());} 2. Select Database mysql_select_db ("test");//3. Set operation code (recommended) mysql_query ("Set names UTF8")//4. Send instruction SQL (DDL data definition language, such as creating tables, DML data manipulation language, which is the INSERT, UPDATE, DELETE,DQL data Query Language, is a SELECT,DTL data transaction statement, such as rollback commit. $sql = "SELECT * from  liuhuayongblog"; $res = mysql_query ($sql, $conn);//5. Receives the returned result and processes while ($row = Mysql_fetch_row ( $res)) {    var_dump ($row);} 6. Release the resources, close the connection, the database imports the data in memory Mysql_free_result ($res);//This sentence can not, recommend this mysql_close ($conn);

5, details

(1) After using the $res result set, the resources must be released in time.

(2) If there is no mysql_close (), the system will turn off automatically.

(3) Finish $res = mysql_query($sql, $conn), close the connection mysql_close($conn) directly, and have no effect on the program because the $ Res points to the appropriate resource in memory (database data is imported into memory) and does not require a database. This is not the same as Java.

(4) In addition to Mysql_fetch_row ($res), there are three ways to obtain data from $res, respectively:

①mysql_fetch_row ($res), returns an array of indexes;

②MYSQL_FETCH_ASSOC ($res), returns an associative array;

③mysql_fetch_array ($res), returns an indexed array and an associative array (two sets);

④mysql_fetch_object ($res), returns a row of data as an object

6, when the database is modified, the same process and query, using $res = Mysql_execute ($sql, $conn), just change the $sql statement.

PHP database programming---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.