0

我有那个代码:

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

$result = $mysqli->query("SELECT id FROM User");
echo("users: " . $result->num_rows ."<br/>");

function echoUserNum()
{
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}
echoUserNum();

它只打印一次用户数(第一次回声呼叫)。问题出在哪里?

4

1 回答 1

4

$mysqli超出了函数的范围echoUserNum。尝试全局,或通过连接

function echoUserNum()
{
    global $mysqli;
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}

或者

function echoUserNum($mysqli)
{
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}
echoUserNum($mysqli);
于 2012-12-09T02:37:58.253 回答