【问题标题】:Returning specific rows using prepared statements使用准备好的语句返回特定的行
【发布时间】:2009-11-17 10:10:57
【问题描述】:

我有一个问题,我准备好的语句似乎只返回返回的行数而不是行的值。下面是我的代码。我确实为此尝试了谷歌,但它没有告诉我任何事情!如果有人能告诉我我做错了什么以及如何解决它,我将非常感激。谢谢

$query2 = 'SELECT * FROM kids_entry WHERE email = ?';
$stmt2 = $connection->prepare($query2);

// bind the user id as an integer to the first ?
$stmt2->bind_param('s', $email);
$stmt2->execute(); // execute the statement

$stmt2->store_result(); // this call is required for the next operation 

while($row1 = $stmt2->fetch()){
    printf ("%s \n", $entries);
}

编辑

我只是尝试用 while 循环替换 if 并且得到了同样的结果。

编辑 2

添加了新代码后它可以工作,但我如何将它分配给 $variable?

【问题讨论】:

    标签: php mysqli prepared-statement


    【解决方案1】:

    参见 php 文档中的示例: mysqli_stmt_fetch

    /* execute statement */
    $stmt->execute();
    
    /* bind result variables */
    $stmt->bind_result($name, $code);
    
    /* fetch values */
    while ($stmt->fetch()) {
        printf ("%s (%s)\n", $name, $code);
    }
    
    /* close statement */
    $stmt->close();
    

    【讨论】:

    • 你所说的赋值给变量是什么意思,只需声明变量,然后使用bind_result绑定到结果集并调用fetch,变量包含行的值。您应该先阅读文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2015-07-13
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多