PHP connection to MySQL server operation

Source: Internet
Author: User

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

Related Article

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.