将mysql_query的结果放入php数组 [英] Put result from mysql_query in php array

查看:49
本文介绍了将mysql_query的结果放入php数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从MySQL数据库中获取一些结果,并将它们放入这样的数组中:

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

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

我已经尝试过了:

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

这不起作用,当我检查模型是否在数组中时会得到FALSE:

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.

这篇关于将mysql_query的结果放入php数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆