arrays and mysql data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjconnor24
Forum Newbie
Posts: 13
Joined: Wed Oct 29, 2008 6:31 am

arrays and mysql data

Post 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>");
    }
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: arrays and mysql data

Post 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 />';
}
 
cjconnor24
Forum Newbie
Posts: 13
Joined: Wed Oct 29, 2008 6:31 am

Re: arrays and mysql data

Post 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:):)!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: arrays and mysql data

Post 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 />");
}
Post Reply