简体   繁体   English

使用准备好的语句从Select *查询返回结果

[英]Return results from Select * query using prepared statements

I have the following test script to adapt all my current programs to using prepared statements ... can't find the right syntax/structure to read the result rows: 我具有以下测试脚本,以使所有当前程序适应使用预准备的语句……找不到正确的语法/结构来读取结果行:

    $userid = "admin";
    $stmt = mysqli_stmt_init($link);
    if (mysqli_stmt_prepare($stmt, 'SELECT * FROM user_info WHERE userid = ?')) {
        mysqli_stmt_bind_param($stmt, "s", $userid);
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_store_result($stmt);
        $number_of_rows = mysqli_stmt_num_rows($stmt);
        echo "Number of Rows: $number_of_rows<br />";
        $result = mysqli_stmt_get_result($stmt);
        for($i=0;$i<$number_of_rows;$i++){  
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
            echo $row["id"];
        }
        mysqli_stmt_close($stmt);
    }
    else{
        // Catch a database error here
        die("Could not query database.");
    }

How do I reference the result correctly (using procedural)? 如何正确引用结果(使用程序)?

Try this: 尝试这个:

$userid = "admin";
$stmt = mysqli_stmt_init($link);
if (mysqli_stmt_prepare($stmt, 'SELECT * FROM user_info WHERE userid = ?')) {
    mysqli_stmt_bind_param($stmt, "s", $userid);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_store_result($stmt);
    $number_of_rows = mysqli_stmt_num_rows($stmt);
    echo "Number of Rows: $number_of_rows<br />";
    $result = mysqli_stmt_get_result($stmt);

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))  
        echo $row["id"];
    }

    mysqli_stmt_close($stmt);
}
else{
    // Catch a database error here
    die("Could not query database.");
}

Look at php manual for get-result function: http://php.net/manual/en/mysqli-stmt.get-result.php (Example #2 Procedural style) 查看php手册中的get-result函数: http : //php.net/manual/en/mysqli-stmt.get-result.php (示例2程序风格)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM