My query keeps failing to fetch. It works just fine in my SQL Console so I’m at a loss. Here’s the code.
$q_st = "SELECT tryouts.playerFName,tryouts.playerLName,tryouts.mainEmail,
tryouts.secondEmail, players.number FROM tryouts LEFT JOIN players ON
players.userID=tryouts.userID WHERE players.team = ?
ORDER BY tryouts.playerLName";
$stmt = $GLOBALS['m']->prepare($q_st);
$stmt->bind_param("s",$GLOBALS['c_team']);
$stmt->execute();
$stmt->bind_result($pfn,$pln,$em,$em2,$num);
if($stmt->fetch()==false)
{
echo($GLOBALS['m']->error . $GLOBALS['m']->sqlstate ." Haha It doesn't work and YOU don't know why!!!");
}
else
{
while($stmt->fetch()) {
static $count3;
echo ($pfn . " " . $pln . " " . $num . "<input type=\"checkbox\" class=\"player\" value=\"" . $em ."\" id=\"player ". ++$count3 . "\"><br />");
}
}
Result is “00000 Haha It doesn’t work and YOU don’t know why!!!”
I’ve tested all the other stmt parameters and they all come through with success.
Any ideas?
As the mysqli_stmt_fetch() method can return
nullandfalseyou have to use===to check if the returned value isfalseor not (or if it isnullor not). Otherwise you interpret the return valuenullas an error code, which it isn’t.Additionally your code reads one row from the result with your first
fetch()call, but you don’t handle the returned row in any way. You are dropping the first row of your result set which might not be something you want.If you want to know how many rows the result set has use mysqli_stmt_num_rows.