Creating mysqli queries?

let's say if I have a query select users.user_id, users.fname, users.lname, bios.bio, groups.groupid from users LEFT JOIN bios on users.user_id = bios.userid , then I want to add another table to the state, and then add the where clause at the end. The problem is that when I bind the parameter, he said that β€œthe number of variables does not match the number of variables in the preparation statement”. How can i solve this? Greetings. Example:

  $info = "select users.user_id, users.fname, users.lname, bios.bio, groups.groupid from users LEFT JOIN bios on users.user_id = bios.userid"; $content = $members->prepare($info); if ($_GET['where'] == 'requests') $info .= "LEFT JOIN requests on users.user_id = requests.receiver"; else if ($_GET['where'] == 'referrals') $info .= "LEFT JOIN referrals on users.user_id = referrals.receiver"; $info .= "where users.user_id = ?"; $content->bind_param('s', $_SESSION['token'][1]); $content->execute(); 
+4
source share
1 answer

You change the SQL string after preparing it. Do not do this. Do it like this:

 $info = "select users.user_id, users.fname, users.lname, bios.bio, groups.groupid from users LEFT JOIN bios on users.user_id = bios.userid"; if ($_GET['where'] == 'requests') $info .= " LEFT JOIN requests on users.user_id = requests.receiver"; else if ($_GET['where'] == 'referrals') $info .= " LEFT JOIN referrals on users.user_id = requests.receiver"; $info .= " where users.user_id = ?"; $content = $members->prepare($info); $content->bind_param('s', $_SESSION['token'][1]); $content->execute(); 

Edit: Also, make sure your SQL fragments are separated by spaces, where necessary; The .= operator does not automatically add you space.

+3
source

Source: https://habr.com/ru/post/1395706/


All Articles