我正在尝试使用面向对象的代码在数据库中显示用户(管理),变量加载了正确的连接信息,我的数据库代码是
/* Code to Connect to the Database */
$mysqli = new mysqli($host, $username, $password);
if($mysqli->connect_errno){
echo "Failed to connect to the Database: " . $mysql->connect_error;
}
我用来显示用户的代码是
$query = ("SELECT m_username, m_email, m_fname, m_sname, m_mccode, m_mobile FROM management");
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ($row["m_username"], $row["m_email"], $row["m_fname"], $row["m_sname"], $row["m_mccode"], $row["m_mobile"]);
}
/* Frees the result set */
$result->close();
/* Close the Connection */
$mysqli->close();
}
当我转到包含此代码的页面时,我没有显示任何内容,并且数据库中有用户。
答案 0 :(得分:1)
您还没有提供数据库名称,因此未选择数据库。更改mysqli_connect()
参数:
$db = 'mydbname';
$mysqli = new mysqli($host, $username, $password, $db);
此外,您可以尝试添加MySQL调试消息,同时测试您的脚本:
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ($row["m_username"], $row["m_email"], $row["m_fname"], $row["m_sname"], $row["m_mccode"], $row["m_mobile"]);
}
/* Frees the result set */
$result->close();
} else {
/* Show error message */
echo $mysqli->error;
}
/* Close the Connection */
$mysqli->close();