Mysql tutorial-php link mysql

Source: Internet
Author: User
MySQL uses the C/S structure, and mysqlcommandlineclient acts as a client. If you use a PHP script to process MySQL, PHP acts as the MySQL client. Note: MySQL is not an integrated component of PHP, but an extension component. it must be enabled in php. ini.

MySQL uses the C/S structure, and the mysql command line client acts as a client. If you use a PHP script to process MySQL, PHP acts as the MySQL client. Note that MySQL is not an integrated component of PHP, but an extension component that must be enabled in php. ini.

Three methods for connecting PHP to MySQL:

  • Mysql extension Library
  • Mysqli Extension Library
  • PDO

PHP can process most databases

PHP + MySQL, regarded as a golden partner

Of course, we can still use other databases.

PHP connection to mysql

// Php connection to mysql

1. establish a connection from the client to the server

$ Conn = mysql_connect ("localhost", "root", "123456 ");

// Var_dump ($ conn );

2. select the database mysql_select_db. The first parameter is the database name, and the second parameter is the connection identifier. this parameter can be omitted. If this parameter is omitted, it will find the last opened connection. Return a value, bool,

Mysql_select_db ("wcp", $ conn)

3. execute the corresponding SQL command:

Mysql_query ($ SQL, $ conn)

// 4. close the connection

Mysql_close ($ conn );

The executed operations include CRUD.

Create, select, update, delete, insert

// Set the character set to avoid garbled characters

Mysql_query ("set names utf8 ");

Tip: // error message

Mysql_error ()

// Error message number

Mysql_errno ()

1. correctly process the data transmitted from the form. the format is valid, the range is valid, and there are spaces and special characters. String processing is required.

① Trim function, remove the left and right spaces.

② The TIME obtained by the form is a string, which must be converted to a timestamp.

③ Strtotime () directly converts the string into a timestamp

④ SQL statements should be written in double quotation marks. Variables are enclosed by single quotes

2. the resource identifier returned by the Select query. if the returned result is false due to an error, the returned resource is processed.

Mysql_fetch_array () contains the associated array of the index array

Mysql_fetch_row () retrieves the index array

Mysql_fetch_assoc () takes only the associated array

Mysql_fetch_object () is obtained as an object

Only one query will be performed in the database at a time.

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.