Jump to content

Recommended Posts

Hey,

 

I am having a problem with my PHP code. What I am trying to do is take the image, title and price from the MySQL database and then put it in to an array before displaying but it doesn't work. It works fine if I query only the image field, but it won't let me pull data from the other fields (title and price). This is my code:

 

$sql = mysql_query("SELECT image, title, price FROM products ORDER BY Rand() LIMIT 20") or die(0);

while ($array = mysql_fetch_array($sql)) {
$data[] = $array[0];
}

for($i=0; $i< count($data); $i++) {
        echo $data[$i];
}

 

All it does is show the image field and not the title or price? Probably something simple but it's doing my head in lol. Any help appreciated here.

 

Thanks guys!

Link to comment
https://forums.phpfreaks.com/topic/198424-php-array-problem-simple-but-arrr/
Share on other sites

You are only assigning the image field to the $data array.

 

$sql = mysql_query("SELECT image, title, price FROM products ORDER BY Rand() LIMIT 20") or die(0);

while ($array = mysql_fetch_row($sql)) {
    $data[] = $array;
}

for($i=0; $i< count($data); $i++) {
        echo $data[$i][0] . " " . $data[$i][1] . " " . $data[$i][2] . "<br />";
}

 

Should display the data and give you a rough idea of how it would work.

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.