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))
This question and answers originated from www.stackoverflow.com
Question by (10/16/2011 3:48:27 PM)

Answer

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.

Answer by

Find More Answers
Related Topics  php  mysql  arrays  query
Related Questions
  • create array from mysql query php

    I have a little problem that I don't understand. I have a db that has an owner and type (and more off course). I want to get a list of all the type values that has owner equal to the current user, b…
  • Search in array query result in php/mysql

    I have the mysql results ( $date_options ) as below Array ( [0] => stdClass Object ( [id] => 4 [start_date] => 2010-09-29 ) [1] => stdClass Object ( [id] => 13 …
  • PHP MYSQL Query Array Field

    Ok. I'm using MySQL and PHP. I have a table called "Pictures" and a field in that table called "Tagged." This field is an array of UserIds which I imploded and stored as a string. example: 114,15…
  • MySQL Result Array in PHP

    Let's say I have this query in PHP. $a = mysql_query("SELECT * FROM `users` WHERE `id` = '1' "); Which is nice and all, but I have a problem. How do I view the contents of $a, which I presum…
  • php mysql query strings array

    i am building a string that i check in mysql db. eg: formFields[] is an array - input1 is: string1 array_push(strings, formFields) 1st string and mysql query looks like this: "select * from my ta…
  • Can I put an array inside a Mysql query?

    I have an array : $arrayA = array(1, 2, 3, 4, 5); Can I put an array like putting a constant number inside a query? like this: SELECT id as 'ID', name as 'NAME', '1234' as 'Number'…
  • How to get instance in this array result from Query in PHP?

    I have an array of results from a query in a variable called $result Here is a var_dump() of this variable: http://pastebin.com/pc53Pmgf array(3) { [0]=> object(stdClass)#23 (25) { […
  • Php multi-dimensional array from mysql result

    I have a mysql table that looks like this: id | uid | title | description | parent 1 | 1 | Portraits | desc. | photostream 2 | 1 | Abstract | descr. | photostream and I am trying to bu…
  • Array in php mysql query

    when i use arrays mysql query it's really slow. are there any tricks that makes this faster? e.g: SELECT * FROM posts WHERE type IN ('1','2','5') ORDER BY id ASC takes much longer th…
  • Query time result in MySQL w/ PHP

    Is there a way that I can get the time of a MySQL query (specifically with PHP)? The actual time it took to complete the query, that is. Something such as: Results 1 - 10 for brown. (0.11 seconds…