mysqli bind_param not returning correct data but the query is correct

65 views Asked by At

I have this small piece of code.

echo $token;

$selstmt=$conn->Prepare("SELECT UserID FROM USER WHERE Token LIKE ?");
$selstmt->bind_param('s', $token);
echo $token;
$selstmt->execute();
$selstmt->store_result();
$selstmt->bind_result($userid);
$selstmt->fetch();
echo $userid;
$selstmt->close();

If I remove the bind_param and directly insert the value in the prepare statement, the query works fine. I echo the value of token twice to check if the value is changed but the $token is same and the value is there. So why is this not working?

1

There are 1 answers

0
A l w a y s S u n n y On

This may work for you, if you include the % signs

$sql  = 'SELECT UserID FROM USER WHERE Token LIKE ?';
$stmt = $conn->prepare($sql);
$stmt->execute(array("%$token%"));
#$result = $stmt->fetch();