0

我正在尝试使用面向对象的代码在数据库中显示用户(管理),变量加载了正确的连接信息,我的数据库代码是

/* 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();
}

当我转到包含此代码的页面时,我什么也没有显示,并且数据库中有用户。

4

1 回答 1

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();
于 2013-04-19T04:29:48.887 回答