The next day, PHP notes, rookie level, Novice hope forgive me!

Source: Internet
Author: User

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!

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.