MySQL PHP Syntax
MySQL can be used in many languages, including PERL, C, C + +, JAVA, and PHP. In these languages, MySQL is the most widely used in web development of PHP.
Most of our examples in this tutorial use the PHP language. If you want to understand the Mysql application in PHP, you can visit our PHP using mysqli introduction.
PHP provides a variety of ways to access and manipulate MySQL database records. The PHP mysqli function is formatted as follows:
Mysqli_function (Value,value,...);
The function section of the above format describes the functions of the MySQL function, such as
Mysqli_connect ($connect); Mysqli_query ($connect, "SQL statement"), Mysqli_fetch_array () Mysqli_close ()
The following example shows the syntax for PHP to invoke the MySQL function:
Example (MYSQLI)<?php $retval=Mysqli_function(Value,[Value,...]) if (! $retval ) { die ( " related error message " } // other MySQL or PHP statement ?>
Starting with the next chapter, we'll learn more about MySQL functions.
MySQL connection using MySQL binary mode connection
You can use the MySQL binary method to enter the MySQL command prompt to connect to the MySQL database.
Instance
Here is a simple example of connecting a MySQL server from the command line:
[Email protected]]# mysql-u root-penter password:******
The mysql> Command Prompt window appears after successful login, and you can execute any SQL statement on it.
After the above command executes, the successful login output is as follows:
Welcome to the MySQL Monitor. Commands End With; or \g.your MySQL connection ID is 2854760 to server Version:5.0.9type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.
In the above example, we use the root user login to the MySQL server, of course you can also use other MySQL user login.
If the user has sufficient permissions, any user can perform SQL operations in the Command Prompt window of MySQL.
To exit the Mysql> Command Prompt window, you can use the Exit command as follows:
Mysql> Exitbye
Connect to MySQL using PHP scripts
PHP provides the Mysqli_connect () function to connect to the database.
The function has 6 parameters, returns the connection identity after successfully linking to MySQL, and returns FALSE for failure.
Grammar
Mysqli_connect (Host,username,password,dbname,port,socket);
Parameter description:
Parameters |
Description |
Host |
Optional. Specifies the host name or IP address. |
Username |
Optional. Specifies the MySQL user name. |
Password |
Optional. Specify the MySQL password. |
dbname |
Optional. Specifies the database that is used by default. |
Port |
Optional. Specifies the port number to attempt to connect to the MySQL server. |
Socket |
Optional. Specify the socket or named pipe to be used. |
You can use PHP's Mysqli_close () function to break the link to the MySQL database.
The function has only one parameter for the Mysqli_connect () function to create the MySQL connection identifier returned after a successful connection.
Grammar
BOOL Mysqli_close (mysqli $link)
This function closes the non-persistent connection to the MySQL server that is associated with the specified connection identity. If Link_identifier is not specified, the last open connection is closed.
Tip: mysqli_close () is usually not required because a non-persistent connection that has been opened is automatically closed after the script has finished executing.
Instance
You can try the following instances to connect to your MySQL server:
Connect to MySQL<?php $dbhost=‘localhost:3306‘;//MySQL Server host address $dbuser=‘Root‘;//MySQL User name $dbpass=‘123456‘;//MySQL User name password $conn=Mysqli_connect($dbhost,$dbuser,$dbpass);If(!$conn ) { die ( "could not connect: " Span class= "Hl-code". mysqli_error (} echo ' database connection Successful! mysqli_close ( $conn ?>
MySQL PHP syntax; MySQL connection