PHP Access MySQL Database

Source: Internet
Author: User

Chapter 9 PHP access to MySQL Database

1. PHP process for accessing MySQL database Server

prior to learning MySQL, the " client / server " architecture was used

Mysql>select * from A; --------"MySQL database server

《 ---------

When PHP accesses the MySQL database, our php serves as the client's role

PHP serves as the role of the client-----------"MySQL database server

《-----------

Next we can use the phpinfo () function to determine if the mysql module is installed

in the within Mysql, the returned data can be broadly divided into two categories:

Insert,update,Delete(DML statement): Returns TRUE if the class statement executes successfully , otherwise it returns FALSE

Select(DQL statement): Returns the result of the query, which is called the result set (which is the collection of results)

PHP Operations Database steps: In fact, and the client operation of the database is the same as the steps

Connecting to a database

|

Select Database

|

Row is affected-- execute SQL statement---- return result set

\                                                   |

\ Processing result set

|

Close connection

2. Connect MySQL server in PHP Script

① Connection Database

in the PHP 's mysql extension gives us a function

Mysql_connect ("Host name", "username", "password")

Is the same syntax that we used to connect to the database through the MySQL client .

As follows:

in the PHP is connected to the database and the same as the client

If the connection is wrong, the reported error message is the same as the client

because the MySQL database module is now being phased out, it is now basically using mysqli or using PDO

@: Error masking operator can mask error messages

once the database is connected, we can pass a series of MySQL extension function to view information about the database

Mysql_get_client_info (): Get client information

Mysql_get_host_info (): Get host information

Mysql_get_proto_info (): get protocol information for MySQL

Mysql_get_server_info (): Get server information in MySQL

② Selecting a Database

mysql_select_db ("Database name", database resource variable )

This step is the same as the use database name operation in MySQL

③ sending SQL Statements

Mysql provides the mysql_query () function to execute the SQL statement, after executing the statement, if the result set is returned, then the variable that receives the result set is a resource type variable

returns if the executed statement has no result set returned. TRUE or FALSE, so the data type of the variable that naturally receives the return value is also a Boolean type

④ Processing Result Sets

There are 4 functions for working with result sets :

Mysql_fetch_row (): returns a record in the result set and saves it as an indexed array

MYSQL_FETCH_ASSOC (): returns a record in the result set and saves it as an associative array

Mysql_fetch_array (): returns a record in the result set and saves it as an index and an associative array,Mysql_both,mysql_num,mysql_assoc

Mysql_fetch_object (): returns a record in the result set as an object

prints all the data in the result set, requiring the outer layer to use a while statement

Print the data as a tabular output

⑤ Close the connection and release the resource

Resources can be freed through the Mysql_free_result() function

to close a database connection by Mysql_close

Next, we will introduce some of the more commonly used mysql extension functions

Mysql_field_name (): Get field name

Mysql_num_rows (): Gets the number of rows in the result set

Mysql_num_field (): Gets the number of columns in the result set

Mysqli

Mysqli is an upgrade on the basis of MySQL. The biggest improvement is the use of object-oriented thinking, the previous MySQL is process-oriented, like C language.

Mysqli uses object-oriented thinking, but can still use functional programming

PHP Access MySQL Database

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.