Home > Software engineering >  SQL Query with Sessions
SQL Query with Sessions

Time:12-06

Hi I have a SQL Query that I had working but now as I try add in Sessions to the query its no longer working.

I want all of the details of the user logged in to appear.

$sql= "SELECT user.username,books.bid,name,authors,edition,approve,issue,issue_book.return 
FROM user inner join issue_book 
ON user.username=issue_book.username inner join books ON issue_book.bid=books.bid WHERE 
issue_book.approve !='' and
issue_book.approve !='Yes' and  username ='$_SESSION['login_user']'
ORDER BY `issue_book`.`return` DESC";

The error that I am currently having is a syntax error, unexpected string content "", expecting "-" or identifier or variable or number. The issue seems to be around:

and username ='$_SESSION['login_user']'

CodePudding user response:

change this username ='$_SESSION['login_user']'

to username =$_SESSION['login_user']

CodePudding user response:

Ok, the thing is, you need to use the concatenate operator to concatenate the variable with the SQL query.

This should work

issue_book.approve !='Yes' and  username = ".$_SESSION['login_user'];

Refer this too - how to use session value in sql query in php ?

Php Concatenate - enter link description here

CodePudding user response:

Assign the session to a variable in front and use it in the query like this,

$session_value = $_SESSION['login_user'];
$sql= "SELECT user.username, books.bid FROM user INNER JOIN issue_book ON user.usernme = issue_book.username ON user.username=issue_book.username inner join books ON issue_book.bid=books.bid WHERE issue_book.approve !='' and issue_book.approve !='Yes' and  username = ".$session_value." ORDER BY `issue_book`.`return` DESC";
  • Related