flit Posted April 8, 2008 Share Posted April 8, 2008 Hi there, Can anyone help me to print mysql data with php? I stored the id's in a session and I want to use those id's and call mysql information with it. Example: $query = "SELECT * FROM books WHERE id="."$-->1 id of session;"; -->??? -->??? -->??? Quote Link to comment Share on other sites More sharing options...
rondog Posted April 8, 2008 Share Posted April 8, 2008 check this out..really helpful examples for beginners : http://www.w3schools.com/PHP/php_mysql_intro.asp but basically for your session thing you do: <?php $id = $_SESSION['sessionvar']; $query = "SELECT * FROM books WHERE id='$id'"; or $query = "SELECT * FROM books WHERE id='".$_SESSION['sessionvar']."'"; ?> something like that. Quote Link to comment Share on other sites More sharing options...
flit Posted April 8, 2008 Author Share Posted April 8, 2008 Thanks rondog, it works, but I encountered another problem, there are different id's in the session, the session file looks like this: sessionvar|s:55:"2,1,2,1,3,2,2,2,1,2,1,3,2,1,1,2,1....... -> something like that When I echo the query it looks like this: SELECT * FROM books WHERE id='2,1,2,1,3,2,2,2,1,2,1,3,2,1,1,2,1.......' This mysql command will return an error[i think] I want to be able to select the 1st id[2] and print to a page then the next id[1] and print to a page then the next id[2] and print to a page then ... then ... then ... Quote Link to comment Share on other sites More sharing options...
flit Posted April 8, 2008 Author Share Posted April 8, 2008 I thought a solution like this $id = array($id); $query = "SELECT * FROM books WHERE id='$id[0]'"; ........-> but it doesn't work Quote Link to comment Share on other sites More sharing options...
angelleye Posted April 9, 2008 Share Posted April 9, 2008 You just need to loop through them all. First, split them based on the comma. That will give you an array of all of the values. Then loop the array to do any SQL stuff within the loop. This should give you a good idea... <?php $SessionVarSplit = explode(',', $_SESSION['sessionvar']); foreach($SessionVarSplit as $ID) { $query = "SELECT * FROM books WHERE id = '" . $ID . "'"; // display records and print page } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.