我正在尝试将 $countries
连接到一个数组,该数组将显示数据库中的所有数据,它只显示我输入的最后数据。我是否可以显示所有数据?
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT product_id, product_name , quantity FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries = $tom;
}
} else {
echo "0 results";
}
$conn->close();
最佳答案
因为您以错误的方式将数据分配给$countries:
$countries = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tom = array(" ". $row["quantity"] . $row["product_name"]);
$countries[] = $tom; // use []
}
} else {
echo "0 results";
}
关于php - 如何使用数组显示数据库中的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37187917/