mysql_fetch_array return only one row
Ok, I have the following code:
$array = mysql_query("SELECT artist FROM directory WHERE artist LIKE 'a%'
OR artist LIKE 'b%'
OR artist LIKE 'c%'");
$array_result= mysql_fetch_array($array);
Then, when I try to echo the contents, I can only echo $array_result[0];
, which outputs the first item, but if I try to echo $array_result[1];
I get an undefined offset.
Yet if I run the above query through PHPMyAdmin it returns a list of 10 items. Why is this not recognized as an array of 10 items, allowing me to echo 0-9?
Thanks for the help.
Solution 1:
That's because the array represents a single row in the returned result set. You need to execute the mysql_fetch_array()
function again to get the next record. Example:
while($data = mysql_fetch_array($array)) {
//will output all data on each loop.
var_dump($data);
}
Solution 2:
You should be using while
to get all data.
$array_result = array();
while ($row = mysql_fetch_array($array, MYSQL_NUM)) {
$array_result[] = $row;
}
echo $array_result[4];