1

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...

1 Answer 1

3

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

4 Comments

oh.. well I don;t like that, not that it matters. I'm still getting the same error as below, however: Parse error: syntax error, unexpected '$set' (T_VARIABLE) //where $set is first used
If you want a giant list of ids, you have to manually add them to an array. see my update
Do I need an index to tell where to put $row[0] in the other array?
@user1801821 No, $giant_list_of_ids[] = value is idiomatic PHP for "add to the end of the array"

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.