Operation mysql database in php

In the previous back-end interaction, we might use to operate with php mysql database, compiled the following steps.

First, we have to have its own database, where my database name is mystu.

1, want to establish a connection php files and databases, we need to log into the database in the php file by mysqli () method, as follows:

$link = @new mysqli("localhost:3306","root","root","mystu");

the mysqli () method receives four parameters, the first address, the name of the second and third personal user name and password, respectively, the fourth to the database operations.

2, sending commands to the mysql database

 $q = "SELECT * FROM mystu";
  $res = $link->query($q);

Here we define a variable used to store the database command execution you need, then by > query ($ q) $ link- sent to the database.

3, parse the returned data

while($arr = $res->fetch_assoc()){
            print_r($arr);
            // echo json_encode($arr);
            echo $arr["sex"];
            echo "<br>";
        }

Analytical data returned by FETCH_ASSOC () method may return data out of solution as an associative array, each array will parse out a, by circulating the data in the database are out traversal.

Guess you like

Origin www.cnblogs.com/mengshou/p/11507729.html