How to use a PHP variable in Inner Join MYSQL query? -


i have been trying past day mysql recognize php variable, have had no luck far.

the code:

...connect db... session_start(); //calls session stored variable $currentusr= $_session['username']; //sql query $sql= 'select users.username, books.* users        inner join userlinkbooks lb        on users.username = lb.username        inner join books        on lb.bktitle = books.title        users.username = "$currentusr"'; $result=mysqli_query($conn,$sql);                                                                 //error check if (!$result) {                                                                                       printf("error: %s\n", mysqli_error($conn));     exit();} //display row while($row=mysqli_fetch_array($result)){                                                                                                             echo "<strong>".$row['title']."</strong>".$row['description']."</br>";}    

my issue $currentusr not calling username passed. after doing error check on it, seems empty.

what not understand when use code :

$sql = "select * users `username`='$currentusr'"; 

the variable processed , works fine, calling book title's , description perfectly. also, if manually type in: users.username = "bill"'; works fine.

some of other errors i've gotten various attempts are:

  users.username = '.'$currentusr';   error: unknown column '$currentusr' in 'where clause' 

or

  users.username = '.$currentusr;     error:  unknown column 'bill' in 'where clause' 

any appreciated. thanks

your variable in single quoted string, preventing interpolation. can try:

$sql = "select users.username, books.* users         inner join userlinkbooks lb         on users.username = lb.username         inner join books         on lb.bktitle = books.title         users.username = '" . $currentusr ."'"; 

using concatenation makes code more readable in opinion. having said that, should using parameterized queries cut down on injection issues. mysqli has such capabilities.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -