468,249 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

select single column from mysql into array

Is there a way to select a single column from a mysql database and
directly put the results into an arrray? Here is the workaround below,
but I would like to elimniate the array_push step:

$uids=array();
$res = mysql_db_query("db", 'select uid from tbl;', $link);
while ($row = mysql_fetch_row($res)) {
array_push ($uids, $row[0]);

}

Feb 12 '06 #1
3 13902
ve*****@yahoo.com wrote:
Is there a way to select a single column from a mysql database and
directly put the results into an arrray? Here is the workaround below,
but I would like to elimniate the array_push step:

$uids=array();
$res = mysql_db_query("db", 'select uid from tbl;', $link);
while ($row = mysql_fetch_row($res)) {
array_push ($uids, $row[0]);

}


Sorry, no.

You can do similar things, but you always end up calling
mysql_fetch_rows() or mysql_fetch_array().

mysql_query (not mysql_db_query as you have) returns a result object or
false, not an array. You must retrieve your data from the result
object, whether you have one column (or row) or hundreds.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 12 '06 #2
> array_push ($uids, $row[0]);

Note: If you use array_push() to add one element to the array it's
better to use $array[] = because in that way there is no overhead of
calling a function.

$uids[] = $row[0];

Feb 12 '06 #3
ve*****@yahoo.com wrote:
Is there a way to select a single column from a mysql database and
directly put the results into an arrray? Here is the workaround below,
but I would like to elimniate the array_push step:

$uids=array();
$res = mysql_db_query("db", 'select uid from tbl;', $link);
while ($row = mysql_fetch_row($res)) {
array_push ($uids, $row[0]);

}


Not directly, but if you are using PEAR:DB, you could use
DB::getCol() for this:

http://pear.php.net/manual/en/packag...mon.getcol.php

/Marcin
Feb 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Alex Glaros | last post: by
3 posts views Thread by Funnyweb | last post: by
21 posts views Thread by John Fabiani | last post: by
reply views Thread by Verdon vaillancourt | last post: by
reply views Thread by Hans Maurer | last post: by
3 posts views Thread by Hendry Taylor | last post: by
2 posts views Thread by Chris Plowman | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.