Front-end PHP entry -033-Connect database-Tianlong eight steps

Source: Internet
Author: User
Tags check character server port

Does PHP check for MySQL support turned on?

If you do not see the mysqli extension under Windows Server, open the php.ini file, open the Php_mysqli.dll!

注意:

MySQL extensions are no longer supported by default from PHP7, that is, mysql_* series functions are no longer supported. Please use mysqli to connect to the database.

MYSQLI support PHP5 also supports PHP7.

Database Connection Tianlong Eight step one step: Connect to the database server
type Description
Function Mysqli_connect
Function Connecting to a MySQL database server
Parameter 1 Host
Parameter 2 Database Server login Name
Parameter 3 Password
Parameter 4 Name of the database
Parameter 5 Database server port does not fill default 3306

If parameter 4, the database name is filled and selected in this step, no third step is required.

Step Two: Judging errors
type Description
Function Mysqli_errno
Function Return connection error number, no error returned 0
Parameter 1 Resources returned by incoming Mysqli_connect
type Description
Function Mysqli_error
Function Returns the connection error string
Parameter 1 Resources returned by incoming Mysqli_connect
Step three: Select a database
type Description
Function mysqli_select_db
Function Select a database in this connection
Parameter 1 Resources returned by incoming Mysqli_connect
Parameter 2 The name of the database that needs to be connected

If you have completed the first step of the database, you do not need to change to another database, you do not need to perform a third step.

Fourth step: Setting the character set
type Description
Function Mysqli_set_charset
Function Set the connection with MySQL power, result, check character set
Parameter 1 Resources returned by incoming Mysqli_connect
Parameter 2 Character set type
Fifth step: Preparing SQL statements

is actually a string of SQL statements.

For example:

sq L="INseRTINTouseR(useRName,PassWoRd )vaLues (the s q l = " i n s e R t i n t o u s e R ( u s e R N a m e , p A s s w o r d ) v a l u e s ( the Username ', ' $password ') ";

We usually use variables in SQL statements to assign values. However, the variable or SQL statement error, very difficult to troubleshoot.

We have added this step in the light of actual work experience.

If you make an error while performing this step, we can print out the SQL statement and paste it into the phpMyAdmin or related tool.

If the execution succeeds, it is not an issue with the SQL statement. If execution fails, double-check the SQL statement.

Sixth step: Send SQL statements
type Description
Function Mysqli_query
Function Send SQL statement
Parameter 1 Resources returned by incoming Mysqli_connect
Parameter 2 Incoming sent SQL statement

The SQL statement is ready to be completed and the SQL statement needs to be sent to the MySQL server via Mysqli_query.

The MySQL server executes the SQL statement sent over.

Seventh step: Determine whether to perform normal or traverse data read

In the 6th step, the statements for the Select category are sent, and the result output is usually displayed. You need to use a function that iterates through the display data.

type Description
Function Mysqli_fetch_array
Function Gets the data in the result set, returns the array to traverse
Parameter 1 Result variables passed in the query
Parameter 2 Incoming Mysqli_num returns an indexed array, MYSQLI_ASSOC returns an associative array, Mysqli_both returns an index and an association
type Description
Function Mysqli_fetch_assoc
Function Get data in result set, return associative array for convenience
Parameter 1 Result variables passed in the query
type Description
Function Mysqli_fetch_row
Function Get data in result set, return index array for convenience
Parameter 1 Result variables passed in the query
type Description
Function Mysqli_fetch_object
Function Get data in result set, return object for traversal
Parameter 1 Result variables passed in the query
type Description
Function Mysqli_num_rows
Function Returns the total number of results from the query
Parameter 1 Result variables passed in the query
type Description
Function Mysqli_num_rows
Function Returns the total number of results from the query
Parameter 1 Result variables passed in the query
Note Use very little in the actual work, understand
Write

In the 6th step, if you send an INSERT statement, you usually need to get the success of the execution, or get the self-increment ID at the same time.

type Description
Function Mysqli_fetch_field
Function Traversing data rows
Parameter 1 Result variables passed in the query
Modify and delete

In the 6th step, if you are sending statements for the update and delete categories. You only need to determine if the execution succeeds.

We'll show you the data sheets for these common functions.

Eighth step: Close the database
type Description
Function Mysqli_close
Function To close a database connection
Parameter 1 Resources returned by incoming Mysqli_connect

A database connection is a resource type. We told you about the resource types in the previous chapters. Any type of resource that involves a number of resources is opened and closed. This ensures that PHP can process and recycle resources more efficiently.

Therefore, when the database connection succeeds, it does not need to be used. We can close this connection.

Other: Display server information functions
type Description
Function Mysqli_get_server_info
Function Return server information
Parameter 1 Resources returned by incoming Mysqli_connect
type Description
Function Mysqli_get_server_version
Function Back to server version
Parameter 1 Resources returned by incoming Mysqli_connect

Note: The connection of PDO is the kingly way
Mysqli only learn the process of the method can be. In the object-oriented phase, the object usage of mysqli is completely discarded, but the way that the PDO objects connect to the database is used.



From for notes (Wiz)

Front-end PHP entry -033-Connect database-Tianlong eight steps

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.