gpt4 book ai didi

php - 我无法从数据库中获取任何数据输出,我只得到 "0 results"作为 else 语句,这是怎么回事?

转载 作者:行者123 更新时间:2023-11-29 10:43:43 25 4
gpt4 key购买 nike

我在使用 php 代码获取一些数据库信息时遇到了一些麻烦。我收到的只是这条消息:“连接成功0 结果”。这是我的代码,提前感谢您的帮助。

 <?php
$servername = "example";
$username = "example1";
$password = "example2";
$row = array();

$conn = new mysqli($servername,$username,$password);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
if ($result = $conn->query($sql)) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}

mysqli_close($conn);
?>

最佳答案

您应该在创建数据库连接时添加数据库名称。您可以使用 mysqli_num_rows 函数来统计行数。行数。

<?php
$servername = "example";
$username = "example1";
$password = "example2";
$dbname = "your_db_name"; // Specify your db-name here.

$conn = new mysqli($servername,$username,$password,$dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
// Checking if there are some records available.
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}

mysqli_close($conn);
?>

关于php - 我无法从数据库中获取任何数据输出,我只得到 "0 results"作为 else 语句,这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45010341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com