Home >Backend Development >PHP Tutorial >Detailed explanation of mysql connection and query examples in PHP_PHP tutorial

Detailed explanation of mysql connection and query examples in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:46:541023browse

I have read many tutorials on the Internet and only cover part of them. If you connect to the database, you only write the connection data or query only the query. The following article configures the mysql connection from PHP and then queries the data instance.

Let’s talk about PHP linking to MYSQL database

The whole process of linking and querying between PHP and mysql

1. Preparation:

php.ini loads mysql component:
extension=php_mysql.dll; remove
                  extension_dir = “ “ Is the path correct?

2. PHP syntax

PHP link mysql function
mysql_connect: Open MySQL link
                             mysql_select_db: Open a database
@And or die hiding errors and conditions display

Usage: mysql_connect("Host", "Username", "Password")
                    mysql_select_db("Open database", connection identifier);
(If you do not specifically declare the connection identifier, the default is the last connection that is opened.)
                                                                      

3. How to execute a SQL statement

mysql_query (SQL statement, connection identifier);

                                                                                                                                                                                                                             mysql_query is used to send a query to the current database of the database server based on the connection identifier. If the connection identifier is default, the default is the last opened connection.
                     Return value: Returns a result identifier on success, returns false on failure.

4. The difference between the two query functions array/row

Format: mysql_fetch_row(result);

                                                                                                                                                                                                               Mysql_fetch_row Used to save a row of query results to an array. The array subscript starts from 0, and each array element corresponds to a field. Through looping, all query results can be obtained.

Format: mysql_fetch_array(result);

                                                                                                                                                                                                                           Mysql_fetch_array and mysql_fetch_row have basically the same functions, except that in addition to using offsets starting from 0 as index, it can also use domain names as index. Value returns all field values ​​in the next row and saves them in an array, or returns false if there is no row.

5. Garbled code problem

 

6. Example:

//A. Link database
The code is as follows
 代码如下 复制代码

              //A、链接数据库

                 $conn = @ mysql_connect(“localhost”, “数据库用户名”, “数据库密码”) or die(“数据库链接错误”);
                  mysql_select_db(“数据库名”, $conn);
                  mysql_query(“set names ‘utf-8′”); //使用utf-8中文编码;

                 // B、查询数据库测试一下

                     $SQL=”SELECT * FROM `表名` order by 以某字段排序名 desc”;
                     $query=mysql_query($SQL);
                     while($row=mysql_fetch_array($query)){
                            print_r($row );
                     }

Copy code

                   $conn = @ mysql_connect("localhost", "database username", "database password") or die("database link error");
                       mysql_select_db("database name", $conn);
mysql_query(“set names ‘utf-8′”); //Use utf-8 Chinese encoding;

                                                                                                                                                                                                                                         // B. Query the database and test it

This source code entry tutorial is my own creation and is for learning only. If it is used for illegal purposes, the author has nothing to do with it.
http://www.bkjia.com/PHPjc/632916.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/632916.htmlTechArticleI have read many tutorials on the Internet, but they only cover part of it. If you connect to the database, you only write the connection data. Or query only query, the following article configures mysql connection from php and then to the query number...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn