Home  >  Article  >  Backend Development  >  How to query multiple pieces of data in php

How to query multiple pieces of data in php

藏色散人
藏色散人Original
2021-03-25 10:19:193000browse

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.

How to query multiple pieces of data in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn