2by
2by

Reputation: 1093

Put result from mysql_query in php array

I want to get some results from the MySQL database and put them in a array like this:

array("value2", "value2", "value3");

I have tried this:

$models = array();
$getmodels = mysql_query("select model from cars");
while($res = mysql_fetch_array($getmodels)) {
    $models[$res['model']];
}

This does not work, when i check if the model is in array i get FALSE:

in_array($_REQUEST['model'], $models))

Upvotes: 0

Views: 10272

Answers (1)

Fad
Fad

Reputation: 9858

You were supposed to give each key a value, not turning the values into the keys. Try this:

$models = array();
$getmodels = mysql_query("select model from cars");
while($res = mysql_fetch_assoc($getmodels)) {
    $models[] = $res['model'];
}

This will create an array with numeric index. Each key will have the car's model as the value.

Upvotes: 7

Related Questions