这些是错误:
警告:mysqli :: query():无法在第495行的[目录]中获取mysqli
警告:mysqli :: query():无法在518行的[目录]中获取mysqli 错误:无法执行
SELECT first_name, phone, residence_address FROM sharp_emp WHERE employee_id = 'AA170336'
。
这是我在 testingDD3.php 文件中的php代码:
<?php
include 'db_connection.php';
$conn = OpenCon();
CloseCon($conn);
?>
这是 db_connection.php 文件中的代码:
<?php
function OpenCon()
{
$dbhost = "localhost"
$dbuser = //hidden username
$dbpass = //hidden password
$db = "sharp_db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
这是 testingDD3.php 中我html中的php代码段:
<div class="layer55">
<h3>
<?php
$sql = "SELECT first_name, phone, residence_address FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<th> first_name </th>";
echo "<th> phone </th>";
echo "<th> residence_address </th>";
echo "</tr>";
while ($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['residence_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
$result->free();
} else {
echo "No records matching your query were found.";
}
} else {
echo "ERROR: Could not able to execute $sql. " . $conn->error;
}
?>
</h3>
</div>
数据库名称为 sharp_db ,表名称为 sharp_emp ,表中已经有数据。 我只想从数据库中获取数据并将其显示给用户即可。
我可以知道为什么有错误吗?我在Google上搜索并试图找到解决方案,据我所知,这与 include 部分有关?但我还是将其更改为 include_once ,仍然是错误。
答案 0 :(得分:-1)
开始时,我打开了连接,然后关闭了它。这就是为什么我无法执行任何数据库查询的原因。
<?php
include 'db_connection.php';
$conn = OpenCon();
CloseCon($conn);
?>
您可以通过输入以下代码来关闭连接
<?php
CloseCon($conn);
?>
在所有查询之后,在代码末尾。
也感谢@catcon。