Page 1 of 1

arrays and mysql data

Posted: Fri Nov 28, 2008 4:24 am
by cjconnor24
Hi all,

I'm after a bit of help with arrays.

I have established a connection to the data base by using the code below and i have selected all the info i need from the database ($sql).

However, i have a list of ids in a session related to the query below.

How can i retrieve the name using the id's from my session?

Any help would be GREATLY appreciated!

Thanks,
C :banghead:

Code: Select all

 
$sql = mysql_query("SELECT id, name FROM products");
$names = mysql_fetch_array  ($sql);
 
    if(!$sql) {
    die("<p>There was an error running the query.</p>");
    }
 

Re: arrays and mysql data

Posted: Fri Nov 28, 2008 5:02 am
by Mark Baker

Code: Select all

 
$sql = mysql_query("SELECT id, name FROM products")
    or die("<p>There was an error running the query.</p>");
while ($row = mysql_fetch_array($sql))
   echo 'ID value '.$row['id'].' is '.$row['name'].'<br />';
}
 

Re: arrays and mysql data

Posted: Fri Nov 28, 2008 5:18 am
by cjconnor24
That's great, thanks for your help.

But do you know how i would get a result depending on the id from the session?

here is how i get the id from the session:
for ($i = 0; $i < count($_SESSION['cart']); $i++){
echo $_SESSION['cart'][$i]." ";
}
do you know how then i could use the id from the session to display information relating to that id in the database?

Thanks again:):)!

Re: arrays and mysql data

Posted: Fri Nov 28, 2008 6:23 am
by Apollo
Just put it as a condition in your SQL query. If $sessionID = the id from the current session:

Code: Select all

$sid = mysql_real_escape_string($sessionID); // for security (i.e. against hackin' smartasses)
$sql = mysql_query("SELECT product,amount,whatever FROM cartcontents WHERE id='$sid' ")
 or die("<p>There was an error running the query.</p>");
while ($row = mysql_fetch_assoc($sql))
{
   print("You have $row[amount] kilograms of $row[product] in yer shopping cart<br />");
}