Why do bind_param()
inside my while
loop doesn't work?
I get
Fatal error: Call to a member function bind_param() on a non-object in ...
What I'm going to do here is to show all data from user
table (if $key
not set) then,
I want to differentiate shown value using if
statement. I have tried to execute prepared query in database manager, and it works. But in this case.. my query seems return 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. 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();