Home > Article > Backend Development > How to query multiple pieces of data in php
How to query multiple pieces of data in php: first create a PHP sample file; then create a PHP MySQL connection; finally, use the "SELECT id, firstname, lastname FROM MyGuests" statement to query multiple pieces of data.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
PHP MySQL native SQL query multiple data (MySQLi - Object-oriented)
<?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(); ?>
[Recommended learning: PHP video tutorial][Recommended: MySQL video tutorial]
The above is the detailed content of How to query multiple pieces of data in php. For more information, please follow other related articles on the PHP Chinese website!