0

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

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Do not use mysql_fetch_array() if you do not use the number indizes but mysql_fetch_assoc() instead. But even better would be to use mysqli_*() instead of mysql_*().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.