why is the php result an array of arrays for a mySQL query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    why is the php result an array of arrays for a mySQL query

    I'm trying to figure out how to return just an array from a mySQL query with this php code.

    Code:
    <?php
    $query = 'Select myField from myTable where id = "'.$thisValue.'"';
    $link = new mysqli("localhost", "my_user", "my_password", "world");
    $result = mysqli_query( $link , $query  );
    $c = mysqli_fetch_all( $result , $resulttype = MYSQLI_NUM );
    ?>
    The result from the query looks like this.
    array(
    [0] =>array([0] =>4337)
    [1] =>array([0] =>4910)
    [2] =>array([0] =>5129)
    [3] =>array([0] =>5672)
    [4] =>array([0] =>5761)
    )
    which means I have to reference the value like this $a[$i][0] instead of just $a[$i]

    How do I return a single array for a multiple rowset?

    I would like the response to look something like this:

    array(
    [0] =>4337
    [1] =>4910
    [2] =>5129
    [3] =>5672
    [4] =>5761
    )
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Is there a reason why it has to be returned as a 1 dimensional array?

    What happens if you need to return 2 fields?

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      For this specific purpose, I am returning just one field. So to use the data, I must first loop through it to eliminate the inner array. I would like to eliminate that processing step.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        in PDO you’d use the FETCH_COLUMN fetch mode. MySQLi has AFAIK no way to do something similar (except for using the given fetch_*() methods) so you probably have to run array_map() on your result array.

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Thanks dormilich, I'll take a look at that.

          Comment

          Working...