简体   繁体   English

准备好的语句,重新使用时失败

[英]Prepared Statement, Fails when re-used

I have this code, works fine when looped through once. 我有这段代码,循环一次后工作正常。

<!-- language: php -->
<?php
$query = "SELECT username FROM users WHERE user_id = ? LIMIT 1";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param("s", $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username);
?>

When I call it for the first time on the page, it loads fine and the data shows as it should. 当我在页面上第一次调用它时,它会很好地加载,并且数据会按预期显示。

<!-- language: php -->
<?php
    // This one works fine.
        while ($stmt->fetch()){
        echo 'Welcome '.$username;
        }
    ?>

If I want to re-use the same query somewhere else, it fails wihtout errors. 如果我想在其他地方重用相同的查询,它将失败,并且不会出错。 Am I doing it wrong? 我做错了吗? What is the correct way to re-use the same query multiple time on the same page, Since the data is the same, I don't want to re-query the DB each time. 在同一页面上多次重复使用同一查询的正确方法是什么,由于数据相同,所以我不想每次都重新查询数据库。

<!-- language: php -->
<?php
// When re-used somewhere else on the page, it fails. Nothing shows.
    while ($stmt->fetch()){
    echo 'Welcome '.$username;
    }
?>

fetch returns one row after another. fetch返回一行。 Once you fetch all rows, any further call to fetch() method will return false . 一旦获取了所有行,对fetch()方法的任何进一步调用将返回false

You need to re- execute() the query in order to get the same result again. 您需要重新execute()查询才能再次获得相同的结果。 This will go to call database again though. 不过,这将再次调用数据库。 If you want to cache the result, you need to put it into some in-memory cache / global variable / whatever you prefer. 如果要缓存结果,则需要将其放入内存中的缓存/全局变量/您喜欢的任何内容中。

If you really want to get to the beginning of result set again, you can use mysqli_data_seek() for this: 如果您确实想再次返回结果集的开头,则可以使用mysqli_data_seek()进行此操作:

mysqli_data_seek($stmt, 0);
while ($stmt->fetch()){
    echo 'Welcome '.$username;
}

You could pull the query result into an array with fetchAll 您可以使用fetchAll将查询结果拉入数组

$users = $stmt->fetchAll();

// this works
foreach($users as $user) {
    echo 'Welcome ' . $username;
}

// this works again
foreach($users as $user) {
    echo 'Welcome ' . $username;
}

More information here: http://php.net/manual/fr/pdostatement.fetchall.php 此处的更多信息: http : //php.net/manual/fr/pdostatement.fetchall.php

What you can do is get the result-set as an ASSOC Array and save the result in a variable. 您可以做的是将结果集获取为ASSOC数组 ,并将结果保存在变量中。 Now you can use this array as many times as you want. 现在,您可以根据需要多次使用此数组。

Update your code like this, 像这样更新您的代码,

$stmt->setFetchMode(PDO::FETCH_ASSOC);
$results = $stmt->fetchAll();

foreach ($results as $r) {
    //first time use
}

foreach ($results as $r) {
    //second time use
}

I needed to split up the code to achieve what I needed with MySQL(i). 我需要拆分代码以实现MySQL(i)所需的功能。 Thanks for your inputs. 感谢您的投入。

<?php
// Preparing the statement
$query = "SELECT username FROM users WHERE user_id = ? LIMIT 1";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param("s", $user_id);
$stmt->store_result();
?>


<?php
// Executing it when needed and creating some variables for later re-use
    $stmt->execute();
    $stmt->bind_result($username);
    while ($stmt->fetch()) {
    $new_username = $username;
    echo 'Welcome '.$new_username;
    }
?>

<?php
// It is possible now to re-use the same result set
    echo 'Welcome '.$new_username;
?>

You can store the results: 您可以存储结果:

<?
$query = "SELECT username FROM users WHERE user_id = ? LIMIT 1";
$stmt = $con->prepare($query) or die(mysqli_error($con));
$stmt->bind_param("s", $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($col['username']);

$result = array();
for ($i = 0; $i < $stmt->num_rows; $i++){
    $stmt->data_seek($i);
    $stmt->fetch();
    foreach ($col as $key => $value){
        $result[$i][$key] = $value;
    }
}
?>

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

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