简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM