php怎么查询多条数据
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视频教程】【推荐:MySQL视频教程】