Jump to content

How to print mysql data with php


flit

Recommended Posts

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;";

 

-->???

-->???

-->???

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ...

 

 

Link to comment
Share on other sites

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
} 
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.