2

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.

1
  • I know that % is the usual wildcard, but have you tried using * instead? Commented Aug 9, 2011 at 18:52

3 Answers 3

20

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a bunch. Oddly enough, when I use this, it doesn't get the value for the data in first row. I.e. Say first artist is Apple, the second is Banana, the third is Cantaloupe. When I var dump, it just does Banana -> Cantaloupe -> etc, no apple.
@Ando: Does it get 'apple' when you use PMA (PhpMyAdmin)? Could be: (1) Your php-generated SQL query doesn't filter the records appropriately. (2) You don't ORDER BY, but you LIMIT, thus maybe the record with 'apple' is left behind while filtering.
My SQL query does bring back Apple, and I do ORDER BY artist ASC. What is peculiar is if I ORDER BY desc, then it shows Apple at the bottom, but then it is missing the item that should be first...very odd
Also, if I use while(list($artist) = mysql_fetch_array($result)) { echo $artist; } the list shows only items 1-9 instead of 0-9.
Try posting the exact code, table description (SHOW TABLE STATUS LIKE "tbl_name"; DESCRIBE tbl_name;) and output, we'll try and see where's the problem.
5

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];

3 Comments

The data is stored in PHP's memory space anyway. Copying that data is usually useless.
@Dor I fail to see how moving the data into an array is useless. How else are you going to access the data? Suppose you want the two rows, you're not going to be able to get both of them in the same iteration of the while loop, so it's going to be a good idea to first copy the data into a usable array.
@DaveChen i think you're right. this feels like a shortcoming of php.
1

I prefer to use this code instead:

$query  = "SELECT artist FROM directory WHERE artist LIKE 'a%' 
        OR artist LIKE 'b%' 
        OR artist LIKE 'c%'";           
$result = mysql_query($query) or die(mysql_error());


while(list($artist) = mysql_fetch_array($result))
{
echo $artist;
}

If you add more fields to your query just add more variables to list($artist,$field1,$field2, etc...)
I hope it helps :)

1 Comment

Or: while($data = mysql_fetch_assoc($result)) { extract($data); /*...*/ }. See: php.net/manual/en/function.extract.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.