Jump to content

How to print mysql data with php


flit

Recommended Posts

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.

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

 

 

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

 

 

Archived

This topic is now archived and is closed to further replies.

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