Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 执行$stmt->num\u行时返回0_Php_Mysqli - Fatal编程技术网

Php 执行$stmt->num\u行时返回0

Php 执行$stmt->num\u行时返回0,php,mysqli,Php,Mysqli,我是mysqli的新手。我正在连接mysql数据库,并获取一些数据 $user_id = "CCD_00005"; /* create a prepared statement */ if ($stmt = mysqli_prepare($GLOBALS['conn'], "SELECT user_name FROM user_master WHERE user_id=?")) { /* bind parameters for markers */

我是mysqli的新手。我正在连接mysql数据库,并获取一些数据

$user_id = "CCD_00005";

    /* create a prepared statement */
    if ($stmt = mysqli_prepare($GLOBALS['conn'], "SELECT user_name FROM user_master WHERE user_id=?")) {

        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s", $user_id);

        /* execute query */
        mysqli_stmt_execute($stmt);

        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $user_name);

        /* fetch value */
        mysqli_stmt_fetch($stmt);

        echo("Total Results:".$stmt->num_rows."<br />");

        printf("%s is having name %s\n", $user_id, $user_name);

        /* close statement */
        mysqli_stmt_close($stmt);
 }
我正在获取结果总结果:0 CCD_00005的名称为sdafasdf

为什么我没有得到行计数为1


请帮助。

更改您的执行顺序,如下所示:

$user_id = "CCD_00005";

    /* create a prepared statement */
    if ($stmt = mysqli_prepare($GLOBALS['conn'], "SELECT user_name FROM user_master WHERE user_id=?")) {

        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s", $user_id);

        /* execute query */
        mysqli_stmt_execute($stmt);

        mysqli_stmt_store_result($stmt);        
        echo("Total Results:".mysqli_stmt_num_rows($stmt)."<br />");

        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $user_name);

        /* fetch value */
        mysqli_stmt_fetch($stmt);


        printf("%s is having name %s\n", $user_id, $user_name);

        /* close statement */
        mysqli_stmt_close($stmt);

在检查行数之前,您可能需要先使用$stmt->store_result。从mysqli_stmt::$num_rows:手册中返回结果集中的行数。mysqli_stmt_num_行的使用取决于是否使用mysqli_stmt_store_result来缓冲语句句柄中的整个结果集。如果使用mysqli_stmt_store_result,可能会立即调用mysqli_stmt_num_行。注意:用于关闭问题的重复项位于屏幕右侧的相关区域下。