McMaster Posted April 13, 2010 Share Posted April 13, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/198424-php-array-problem-simple-but-arrr/ Share on other sites More sharing options...
premiso Posted April 13, 2010 Share Posted April 13, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198424-php-array-problem-simple-but-arrr/#findComment-1041207 Share on other sites More sharing options...
McMaster Posted April 13, 2010 Author Share Posted April 13, 2010 Hey man, that was quick and solved it great work mate. Thank you ever so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/198424-php-array-problem-simple-but-arrr/#findComment-1041209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.