Forum Moderators: coopster

Message Too Old, No Replies

Select query and $_SESSION

         

Ann_G

2:20 am on Mar 2, 2006 (gmt 0)

10+ Year Member



How do I get the user_id from my table? This is my code:

$query = "SELECT * FROM books WHERE user_id =($_SESSION['user_id'])";

$result = mysql_query ($query); // Run the query.

while ($row = mysql_fetch_array($result)) {

$book_id = $row['book_id'];
$user_id = $row['user_id'];
$title = stripslashes($row['title'])
$display_block .= "<p><strong>$title</strong>
</p>";
}

This part returns the error:
WHERE user_id =($_SESSION['user_id']

I have two tables. One that registers users and one that records books by the users. I can see that the registration script for the users work. The table for the books records a number for each book and than the number for the user who inputted the book title. But I can't figure out how to retrieve the book titles so that each users can see his book list when he logs in.

Any suggestions are greatly appreciated.

FalseDawn

2:48 am on Mar 2, 2006 (gmt 0)

10+ Year Member



$query='SELECT * FROM books WHERE user_id=' . $_SESSION['user_id'];

I am assuming that user id has been validated as an integer - if not, you'll need to do:

$query='SELECT * FROM books WHERE user_id=\'' . mysql_real_escape_string($_SESSION['user_id']) . '\'';

Also, rather than SELECT *, select only the columns you need - SELECT * is almost always the sloppy alternative.

Habtom

9:55 am on Mar 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The $_SESSION['user_id'] may not be holding any value. Try to echo the $query and see if the query looks ok.

Also change
$query = "SELECT * FROM books WHERE user_id =($_SESSION['user_id'])";

TO

$query = "SELECT * FROM books WHERE user_id =". $_SESSION['user_id'] ."";

OR

$query = "SELECT * FROM books WHERE user_id ='". $_SESSION[user_id] ."'";

And it should work fine.

Habtom

Ann_G

8:38 pm on Mar 2, 2006 (gmt 0)

10+ Year Member



Thank you so much! I very much appreciate the help, I was about to tear my hair out.

This line solved the problem:

$query = "SELECT * FROM books WHERE user_id =". $_SESSION['user_id'] ."";