After our data is stored in the database, we want to connect the data to the Web page, read the data through the interpreter of the Web server, and then pass it to the client page.
Here, I chose PHP as the interpreter for learning. Here is a detailed summary of PHP connection to MySQL server steps.
1). Connect to MySQL server
// Syntax: $conn =mysqli_connect (' IP address ', ' User name ', ' Password ', ' database ', port number),//as set in XAMPP Setup conditions: $conn=mysqli_connect(' 127.0.0.1 ', ' root ', ', ' Fanfan ', 3306);
2). Submit SQL command to MySQL server
To prevent garbled characters, it is common to execute a coded SQL statement once before executing a real SQL command. The execution function is mysqli_query (); There are several ways to execute the results, you can see the next blog post, here is mainly to comb the PHP and SQL connection steps ~
// Set Encoding $sql= "SET NAMES UTF8"; Mysqli_query ($conn,$sql); // real operations, such as querying the student table for student information in Stuid 1 $sql= "SELECT * FROM student WHERE stuid=1"; $result=mysqli_query($conn,$sql);
3). View the results of the execution, if the output is correct, error output SQL command, because the SQL statement is generally wrong, the correctness of the result is judged based on the type of SQL statement returned in the previous step, if it is a query statement, then determine whether it is null.
if ($result= = =trueecho ' succ '; } Else echo ' err '. $sql ; }
4). Close the database (optional, because the connection is automatically closed when the data query is finished)
Mysqli_close ($conn);
The above is the basic operating procedures, generally connected to the database and settings encoding can be saved as a PHP file (init.php), and then need to introduce, code reuse.
require (' init.php ');
It is also important to note that:
The latter involves the introduction of the browser and so on, pay attention to the data transmission format.
How to read the required data from the browser ($_request["]);
Determine the result type.
PHP connection to MySQL server operation