Today, write about process-oriented style, connect database case!
<?php
$conn = Mysqli_connect ("localhost", "root", "" "," php_test ");
Determine if the connection database is successful
if (Mysqli_connect_error ($conn)) {
Die ("Connection failed:". Mysqli_connect_error ());
}
Read data from MySQL database, set encoding
Mysqli_query ($conn, "SET NAMES utf8mb4");
$sql = "SELECT * from Zrzf_teacher";
Execute SQL statement
??????????: $conn->query ($sql); Here query is the method of the instance object;
$result = $conn->query ($sql);
if ($result->num_rows > 0) {
$DATAARR = Array ();
while ($row = $result->fetch_assoc ()) {
Save data
$DATAARR [] = $row;
}
$backJson = Array ("data" = = $DATAARR, "result" = "Congratulations, query Success!");
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die
}else{
$backJson = Array ("Data" =>array (), "result" = "Sorry, no query data!");
$jsonS = Json_encode ($backJson);
Echo $jsonS;
Die
}
$conn->close ();
If you don't know what the above code means or want to see an object-oriented notation, see http://www.cnblogs.com/KennyHito/p/6897387.html
I just contact PHP, is a rookie, if there is no place please leave a message below!
The next day, PHP notes, rookie level, Novice hope forgive me!