PHP implementation of MySQL connection and query example detailed

Source: Internet
Author: User
Tags php and php and mysql table name mysql database

Here is a PHP link to the MySQL database

PHP and MySQL link and query the whole process

1. Compounding:

php.ini load MySQL component:
Before the Extension=php_mysql.dll; Remove
Extension_dir = "" Path is correct

2. PHP syntax

PHP link MySQL function
Mysql_connect: Open MySQL Link
mysql_select_db: Open a Database
@ and Or die hide errors and condition display

Usage: mysql_connect ("host", "username", "password")
mysql_select_db ("Open database", connection identifier);
(If the connection identifier is not specifically declared, it is assumed to be the last open connection.) )

3. How to execute an SQL statement

mysql_query (SQL statement, connection identifier);
Description: mysql_query is used to send a query to the database server's current database based on the connection identifier, which is tacitly considered to be the last open connection if the connection identifier defaults.
Return value: Returns a result identifier after success, false when failed.

4, two kinds of query function Array/row difference

Format: mysql_fetch_row (Result);
Description: Mysql_fetch_row to query the results of the row to save to the array, the array subscript starting from 0, each array element corresponds to a field. By looping, you can get all the results of the query.

Format: mysql_fetch_array (Result);
Description: Mysql_fetch_array and Mysql_fetch_row functions are basically the same, except that it can be indexed by the offset from 0, but also by domain name. Value returns all the field values for the next row and saves them to an array, with no rows returning false.

5, garbled problem

The code is as follows Copy Code
mysql_query ("Set names ' GBK '"); Solve Chinese garbled

6. Example:

The code is as follows Copy Code

A, linked databases

$conn = @ mysql_connect ("localhost", "Database user name", "Database Password") or Die ("Database link error");
mysql_select_db ("Database name", $conn);
mysql_query ("Set names ' utf-8′"); Use utf-8 Chinese code;

B, query the database test

$SQL = "SELECT * from" table name ' ORDER by Desc ' in a field;
$query =mysql_query ($SQL);
while ($row =mysql_fetch_array ($query)) {
Print_r ($row);
}

Source code into the tutorial for my original, only for learning, such as the use of illegal use and the author has nothing to do.

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.