试图使用面向对象的代码来显示数据库信息


Trying to use Object Oriented code to display database information

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

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

当我转到包含此代码的页面时,没有显示任何内容,并且DB中有用户。

您没有提供数据库名称,因此没有选择数据库。修改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();