user1801821
user1801821

Reputation: 21

mysql_fetch_array() not working properly, missing most values

Alright, so I'm trying to get an array of Key values from a mysql table and mysql_fetch_array() won;t seem to work properly. Code:

                        $x="select id from topics"
                    $set=mysql_query($x);
                    echo mysql_num_rows($set);
                    print_r(mysql_fetch_array($set));
                    $ids=mysql_fetch_array($set);
                    echo $ids[0];
                    echo $ids[1];

I've moved stuff all around but nothing seems to be changing the output:

66 //number of values in result set

Array ( [0] => 3 [id] => 3 ) //value(singular) being moved to array

4 //supposed single value of the above array

I'm really not sure what is going on here...

Upvotes: 1

Views: 375

Answers (1)

akatakritos
akatakritos

Reputation: 9858

mysql_fetch_array brings a single row back as a PHP array, indexed by column name and by 0-based index. it DOES NOT load the whole set into a giant array, which is what you seem to be expecting.

You have to iterate over the result set in a loop, like so:

$x="select id from topics";
$set = mysql_query($x);
echo mysql_num_rows($set);
$giant_list_of_ids = array();
while ($row = mysql_fetch_array($set))
{
    echo $row[0]; //or alternatively, echo $row['id'];
    $giant_list_of_ids[] = $row[0];
}

Upvotes: 3

Related Questions