亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP MySQL reads data

Reading data from MySQL database

We have learned to add data to the database. In this section, we will talk about how to read the data from the database and display it on the page?


To query data, use select

## Basic syntax* Example Example description
## Category Detailed explanation
select from table;
select * from MyGuests;
Query MyGuests All results in all fields in the table
Note

: "*" is a regular expression The expression is written to match all

If you want to learn more about SQL, please visit our SQL tutorial.


Instanceus Query the data we added to the MyGuests table and display it on the page

<?php
 header("Content-type:text/html;charset=utf-8");    //設(shè)置編碼
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 
 // 創(chuàng)建連接
 $conn = new mysqli($servername, $username, $password, $dbname);
 // 檢測連接
 if ($conn->connect_error) {
     die("連接失敗: " . $conn->connect_error);
 }
 
 $sql = "SELECT * FROM MyGuests";
 $result = $conn->query($sql);
 
 if ($result->num_rows > 0) {
     // 輸出每行數(shù)據(jù)
     while($row = $result->fetch_assoc()) {
         echo "id: ". $row["id"]. " - Name: ". $row["firstname"]. "   " . $row["lastname"] ."   ".$row['email'] ."<br/>";
     }
 } else {
     echo "0 個(gè)結(jié)果";
 }
 $conn->close();
 ?>

The program running results:

0.png See if it is our MyGuests table The data inside

But if we only want to query two of the fields, such as firstname and email, look at the example below
<?php
 header("Content-type:text/html;charset=utf-8");    //設(shè)置編碼
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 
 // 創(chuàng)建連接
 $conn = new mysqli($servername, $username, $password, $dbname);
 // 檢測連接
 if ($conn->connect_error) {
     die("連接失敗: " . $conn->connect_error);
 }
 
 $sql = "SELECT firstname,email FROM MyGuests";
 $result = $conn->query($sql);
 
 if ($result->num_rows > 0) {
     // 輸出每行數(shù)據(jù)
     while($row = $result->fetch_assoc()) {
         echo  " - Name: ". $row["firstname"]. "--------".$row['email'] ."<br/>";
     }
 } else {
     echo "0 個(gè)結(jié)果";
 }
 $conn->close();
 ?>

We only need to

* Just change it to a specific field:
Program running result:

0.png

Continuing Learning
||
<?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; // 創(chuàng)建連接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢測連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } $sql = "SELECT * FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 輸出每行數(shù)據(jù) while($row = $result->fetch_assoc()) { echo "id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] ." ".$row['email'] ."<br/>"; } } else { echo "0 個(gè)結(jié)果"; } $conn->close(); ?>
submitReset Code