php查詢多條資料的方法:先建立一個PHP範例檔案;然後建立PHP MySQL連線;最後透過「SELECT id, firstname, lastname FROM MyGuests」語句實現多個資料的查詢即可。
本文操作環境:windows7系統、PHP7.1版,DELL G3電腦
PHP MySQL 原生SQL查詢多個資料(MySQLi -物件導向)
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 结果"; } $conn->close(); ?>
以上是php怎麼查詢多個數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!