Prepared Statements - Number of Rows

后端 未结 4 1605
旧巷少年郎
旧巷少年郎 2021-01-04 06:15

I\'m learning about prepared statements and trying to work with a query that yields multiple rows of results. Right now, I\'m just trying to figure out how to determine the

4条回答
  •  醉酒成梦
    2021-01-04 07:13

    num_rows returns the number, you have to store it in a variable.

    /*.....other code...*/
    $numberofrows = $stmt->num_rows;
    /*.....other code...*/
    
    echo '# rows: '.$numberofrows;
    

    So full code should be something like this:

    if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ? ORDER BY id ASC")) 
        {
        /* Bind parameters, s - string, b - blob, i - int, etc */
        $stmt -> bind_param("i", $id);
        $stmt -> execute();
    
        /* Bind results */
        $stmt -> bind_result($testfield1, $testfield2, $testfield3);
    
        /* Fetch the value */
        $stmt -> fetch();
        $numberofrows = $stmt->num_rows;
    
        /* Close statement */
        $stmt -> close();
       }
    echo '# rows: '.$numberofrows;
    

提交回复
热议问题