select single column from mysql into array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • veg_all@yahoo.com

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

    }

  • Jerry Stuckle

    #2
    Re: select single column from mysql into array

    veg_all@yahoo.c om wrote:[color=blue]
    > 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]);
    >
    > }
    >[/color]

    Sorry, no.

    You can do similar things, but you always end up calling
    mysql_fetch_row s() or mysql_fetch_arr ay().

    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.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Sjoerd

      #3
      Re: select single column from mysql into array

      > 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];

      Comment

      • Marcin Dobrucki

        #4
        Re: select single column from mysql into array

        veg_all@yahoo.c om wrote:[color=blue]
        > 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]);
        >
        > }[/color]

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



        /Marcin

        Comment

        Working...