简体   繁体   English

为什么while循环内的bind_param()不起作用? 返回致命错误

[英]Why do bind_param() inside while loop doesn't work? return Fatal Error

Why do bind_param() inside my while loop doesn't work? 为什么我的while循环中的bind_param()不起作用?
I get 我懂了

Fatal error: Call to a member function bind_param() on a non-object in ... 致命错误:在...中的非对象上调用成员函数bind_param()

What I'm going to do here is to show all data from user table (if $key not set) then, 我在这里要做的是显示user表中的所有数据(如果未设置$key ),
I want to differentiate shown value using if statement. 我想使用if语句区分显示的值。 I have tried to execute prepared query in database manager, and it works. 我试图在数据库管理器中执行准备好的查询,并且它可以工作。 But in this case.. my query seems return false. 但是在这种情况下..我的查询似乎返回false。 So, any idea? 那么,有什么想法吗? here is the codes, 这是代码,

function cariTemen($key) {
    require 'settings.php';
    $my_id = $_SESSION['user_id'];

    $stmt = $conn->prepare ("SELECT user_id,nama_asli FROM user WHERE username LIKE ? OR nama_asli LIKE ?");
    $key = '%'.$key.'%';
    $stmt->bind_param('ss',$key,$key);
    $stmt->execute();
    $stmt->bind_result($id,$nama);

    //MODIFIED FROM HERE
    while($stmt->fetch()) {
        $temen_id = $id;
        $username = $nama;

        $temen = $conn->prepare("SELECT id_temenan FROM temenan WHERE temen_id = ? AND user_id = ?");
        $temen->bind_param('ii',$temen_id,$my_id); //ERROR IS HERE
        $temen->execute();
        $temen->store_result();
        $jml= $temen->num_rows;

        if($jml > 0) {

            echo $username.' [Temenan]<br>';

        } else {

            echo $username.' <a href="add.php?user_id='.$temen_id.'"">Temenin</a><br>';
        }
    }

    $stmt->close();
}

Thank you in advance for any help you can provide. 预先感谢您提供的任何帮助。

as marcellorvalle said that mysql can not maintain 2 simultaneous queries, In this case, i need to store all the values into an array() variable. 正如marcellorvalle所说,mysql不能同时维护2个查询,在这种情况下,我需要将所有值存储到array()变量中。 so the script would be like this : 所以脚本应该是这样的:

$user_id = array(); 
$username = array();
$i = 0;

while($stmt->fetch()) {
    $user_id[$i] = $id;
    $username[$i] = $nama;
    $i++;
}

$stmt->close();

for ($j= 0 ; $j <= $i; $j++) {
    $temen = $conn->prepare("SELECT id_temenan FROM temenan WHERE temen_id = ? AND user_id = ?");
    $temen->bind_param('ii',$user_id[$j],$my_id);
    $temen->execute();
    $temen->store_result();
    $jml= $temen->num_rows;

    if($jml > 0) {

        echo  '<a href="profil.php?user_id='.$user_id[$j].'"">'.$username[$j].'</a> [TeMeNan]<br>';

    } else {

        echo ' <a href="profil.php?user_id='.$user_id[$j].'"">'.$username[$j].'</a> <a href="add.php?user_id='.$user_id[$j].'"">[TeMeNin]</a><br>';

    }

}
$temen->close();
  1. It is not bind_param() doesn't work but prepare() . 不是bind_param()不起作用,而是prepare()
  2. It doesn't work because prepared queries are unbuffered by default, and thus in order to run another query while you didn't get all the results from the first one, you have to call store_result() on the first query first. 这是行不通的,因为默认情况下,准备好的查询是未缓冲的,因此,为了在您没有从第一个查询中获得所有结果时运行另一个查询,必须首先在第一个查询上调用store_result()
  3. 99 times of 100 when you do a nested query, it means that you should have been running a single query with JOIN 当您执行嵌套查询时,其100的99倍,这意味着您应该使用JOIN运行单个查询

According to the message $temen seems not to be an object. 根据该消息,$ temen似乎不是对象。 Try to debug it using var_dump($temen). 尝试使用var_dump($ temen)对其进行调试。 It seems the $conn->prepare is returning false for some reason and false is not an object. 似乎$ conn-> prepare由于某种原因返回了false ,而false不是对象。

You can check any error echoing $conn->error to see what happened. 您可以检查任何回显$ conn-> error的错误,以查看发生了什么。

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

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