SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL query results into indexed array

    Newbie question here...I apologize if this has been answered before; a search through the PHP forum threads didn't clear this up any for me.

    I have a query that extracts 4 columns of data from a mySQL database. What I'd like to do is make that query an array in this structure:

    array( array("Name1", 1 , 0, 0),
    array("Name2", 1 , 1, 0),
    array("Name3", 1 , 1, 1)
    );

    And to be able to loop through the data, I'd like to be able to just refer to the array as array[0][0], which would be "Name1." I've read through all the built-in array and mysql functions, and I don't know if there is one built-in that would work, but if so, I'm not sure which one to use.

    This seems like a pretty easy question, but it's been bugging me for hours! If I need to post the query and some of my code, please let me know.

    Thx

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This assumes you're using mysqli, but it'll be the same idea with anything else:
    Code PHP:
    $arr = array();
    while ($row = $result->fetch_row) {
        $arr[] = $row;
    }

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The mysql driver for PDO supports this through the fetchAll() method, but if not using PDO

    PHP Code:
    $arr = array();
    while (
    $row mysql_fetch_row($result)) {
        
    $arr[] = $row;

    Also see mysql_fetch_assoc() and mysql_fetch_array(), if you want associative keys, or both.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correction to my post:
    Code PHP:
    $arr = array();
    while ($row = $result->fetch_row()) {
        $arr() = $row;
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn, that was still wrong:
    Code PHP:
    $arr = array();
    while ($row = $result->fetch_row()) {
        $arr[] = $row;
    }

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The mysql driver for PDO supports this through the fetchAll() method, but if not using PDO

    PHP Code:
    $arr = array();
    while (
    $row mysql_fetch_row($result)) {
        
    $arr[] = $row;

    Also see mysql_fetch_assoc() and mysql_fetch_array(), if you want associative keys, or both.
    This is what I thought would work...but when I want to echo the results, I use this code, and get the result below it:


    Code:
    echo "$arr[0] <br />";
    echo "$arr[1] <br />";
    echo "$arr[2] <br />";
    echo "$arr[3] <br />";
    Array
    Array
    Array
    Array

    If I can get the results into an array of arrays, I would think I could echo it like this and it would pretty much display the entire table, one field at a time, in a long list:

    Code:
    echo "$arr[0][0] <br />";
    echo "$arr[0][1] <br />";
    echo "$arr[0][2] <br />";
    echo "$arr[0][3] <br /><br />";
    echo "$arr[1][0] <br />";
    echo "$arr[1][1] <br />";
    echo "$arr[1][2] <br />";
    echo "$arr[1][3] <br /><br />";
    etc.

    Thx again...arrays have been the toughest thing to get comfortable with in PHP!

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do have an array of arrays.

    PHP Code:
    // works
    $arr[0][0]; 
    But do you need an array? Often you would just use the loop to do the work.
    PHP Code:
    while ($row mysql_fetch_assoc($result)) { 
        echo 
    "
    {$row['foo']}
    {$row['bar']}
    etc...
    "
    ;


  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You do have an array of arrays.

    PHP Code:
    // works
    $arr[0][0]; 
    But do you need an array? Often you would just use the loop to do the work.
    The loop I'm using is actually three nested for loops where I echo (or try to anyway!) each value in the row in a way that works with my Javascript that shows and hides it based on the element id. Values 2, 3, and 4 (which are $arr[$x][1], $arr[$x][2], and $arr[$x][3] depending on the row) are concatenated and make up the id of various elements (icon, checkbox, and div, as i_$arr[$x][1]-$arr[$x][2]-$arr[$x][3] and c_ and d_ similarly for the checkbox and div. Not sure if that makes sense without the whole code. I think I may have figured out a way to make it work, but if not I'll post more of the code.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not really sure what you're doing, but make sure to look here, it may be relevant.
    http://www.php.net/manual/en/faq.htm...aq.html.arrays
    http://www.php.net/manual/en/function.json-encode.php


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •