当前位置:网站首页>PHP MySQL reads data
PHP MySQL reads data
2022-07-03 17:51:00 【Crooning ~ shallow singing】
from MySQL Database reads data
SELECT Statement is used to read data from a data table :
SELECT column_name(s) FROM table_name
We can use * Number to read all the fields in the data table :
SELECT * FROM table_name
To learn more about SQL Knowledge , Please visit our SQL course .
Use MySQLi
In the following example, we start from myDB Database MyGuests Table read id, firstname and lastname Column and display it on the page :
example (MySQLi - object-oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die(" The connection fails : " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Output data while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 result "; } $conn->close(); ?>
The above code is parsed as follows :
First , We set it up SQL Statements from MyGuests Read in data table id, firstname and lastname Three fields . Then we use this SQL Statement takes the result set from the database and assigns it to the copied variable $result.
function num_rows() Judge the returned data .
If multiple pieces of data are returned , function fetch_assoc() Put the associative set into the associative array and output circularly . while() Loop out the result set , And the output id, firstname and lastname Three field values .
The following example uses MySQLi Process oriented approach , The effect is similar to the above code :
example (MySQLi - Process oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die(" The connection fails : " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // Output data while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 result "; } mysqli_close($conn); ?>
Use PDO (+ Preprocessing )
The following example uses preprocessing statements .
Selected MyGuests In the table id, firstname and lastname Field , And on the HTML In the table :
example (PDO)
<?php echo "<table style='border: solid 1px black;'>"; echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\n"; } } $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests"); $stmt->execute(); // Set the result set to an associative array $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?>
边栏推荐
- IntelliJ 2021.3 short command line when running applications
- Getting started with deops
- Applet setting multi account debugging
- [vscode] convert tabs to spaces
- Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
- SSL / bio pour OpenSSL Get FD
- List of financial products in 2022
- Golang单元测试、Mock测试以及基准测试
- UE4 official charging resources, with a total price of several thousand
- i++与++i的区别:通俗易懂的讲述他们的区别
猜你喜欢
Notes on problems -- watching videos on edge will make the screen green
STM32实现74HC595控制
AcWing 271. 杨老师的照相排列【多维DP】
1164 Good in C
Type conversion, variable
(8) HS corner detection
POM in idea XML graying solution
The third day of writing C language by Yabo people
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
Ml (machine learning) softmax function to realize the classification of simple movie categories
随机推荐
聊聊支付流程的设计与实现逻辑
VM11289 WAService. js:2 Do not have __ e handler in component:
Deops入门
Design limitations of structure type (struct)
鸿蒙第三次培训
Keepalived 设置不抢占资源
Introduction to SolidWorks gear design software tool geartrax
PS screen printing brush 131, many illustrators have followed suit
Play with fancy special effects. This AE super kit is for you
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
Kotlin的協程:上下文
Records of long objects and long judgments in the stream of list
国内如何购买Google Colab会员
PHP processing - watermark images (text, etc.)
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
Keepalived setting does not preempt resources
自动渗透测试工具核心功能简述
UE4 official charging resources, with a total price of several thousand
A. Odd Selection【BruteForce】
Graduation summary