如果php mysqli代码应该> 0,则num_rows为0

时间:2012-04-13 23:23:34

标签: php mysqli mysql-num-rows

我的num_rows又回到了0,我试过几次调用它,但是我被卡住了。这是我的代码:

    $conn = new mysqli($dbserver, "dbuser", "dbpass", $dbname);

    // get the data 
    $sql = 'SELECT AT.activityName, AT.createdOn
            FROM userActivity UA, users U, activityType AT  
            WHERE U.userId = UA.userId
            and AT.activityType = UA.activityType
            and U.username = ?
            order by AT.createdOn';             
    $stmt = $conn->stmt_init();
    $stmt->prepare($sql);
    $stmt->bind_param('s', $requestedUsername);
    $stmt->bind_result($activityName, $createdOn);
    $stmt->execute();

    // display the data
    $numrows = $stmt->num_rows;
    $result=array("user activity report for:  " . $requestedUsername . " with " . $numrows . " rows:");
    $result[]="Created On --- Activity Name";
    while ($stmt->fetch())
    {
        $msg = " " . $createdOn . " --- " . $activityName . " ";
        $result[] = $msg;
    }

    $stmt->close();

找到了多行,并且fetch循环处理它们就好了。有什么建议可以让我获得查询中返回的行数?

非常感谢您的建议。提前谢谢。

4 个答案:

答案 0 :(得分:2)

您需要先在$stmt->store_result()之前致电$stmt->num_rows

答案 1 :(得分:1)

尝试在调用num_rows;。

之前添加此项

$stmt->store_result();

答案 2 :(得分:0)

我不知道这是否会解决这个问题,但是如果我没有弄错的话,你<{1>}之前 bind_results execute之后你就不能while ($stmt->fetch()) { $msg = " " . $createdOn . " --- " . $activityName . " "; $result[] = $msg; } 。< / p>

你的while循环:

$msg

在循环的每次迭代中,您将丢失所有$msg .=个变量,但最后一个设置除外,因为您需要执行$msg或使$msg[] =成为数组{{1} }

答案 3 :(得分:0)

$stmt->store_result();
$numrows = $stmt->num_rows;

检查:http://php.net/manual/en/mysqli-result.num-rows.php和此 http://php.net/manual/en/mysqli-stmt.num-rows.php