1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how can i get a sql-result into an array?

Discussion in 'PHP' started by falcondriver, Feb 15, 2007.

  1. #1
    first of all, its no so easy as i sounds.

    lets say i have this database
    SEMrush
    name - address - tel
    --------------------
    cartman - southpark north - 111
    stan - southpark east - 222
    kyle - southpark west - 333
    kenny - south park - 000

    now i need 1 array that contains each col(!) as an array inside, starting with the col name as first value.

    at the end it should look like

    $res = array (
    array('name', 'cartman', 'stan', ...)
    array('address', 'southpark north', 'southpark east', ...)
    array('tel', '111', '222', ...)
    )

    is it possible to build such a function that can create such an array from every valid sql query?
     
    falcondriver, Feb 15, 2007 IP
    Yfcadmin likes this.
    SEMrush
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    you mean you want a function to put your entire database into an array ?

    perhaps if you paste the sort of sql query you want to make that structure from it'll be easier to understand.
     
    krakjoe, Feb 15, 2007 IP
  3. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to loop through those fields 3 times, and you need 4 arrays, first loop you save the names, second you save the address and so on.
    This is an awful way of saving database records into an array though.
     
    hamidof, Feb 15, 2007 IP
  4. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If the cols names are the ones you want to put as the first element of the array, you could do something like this:
    <?php
      $query = 'SELECT name, address, tel FROM table';
      $result = mysql_query($query);
      while ($row = mysql_fetch_assoc($result)) {
        foreach ($row as $column => $value) {
          $array_name = $column;
          $$array_name[0] = $column;
          $$array_name[] = $value;
        }
      }
    ?>
    Beware, totally UNTESTED!! Anyway, hope it helps a bit... ;)
     
    picouli, Feb 15, 2007 IP
  5. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #5
    thanks everyone.
    krakjoe: no, i only need the result of 1 query in an array, not the whole database :)
    picouli: didnt work, but gave me a push into the right direction. what exactly are you trying to do if you use "$$" at $$array_name[]? i know its valid php, but i dont know what happens if yo do this...?

    however, here is the solution i came up with:
    
    function sql2array($sql) {
       global $db;
       $data = array();
       $x = 0;
       $y = 1;
       $res = $db->query($sql);
       while ($row = $db->getdata($res)) {
           $x=0;
           foreach ($row as $col => $value) {
               $data[$x][0] = $col;
               echo $x."-".$y."::".$col." % ".$value."<br>";
               $data[$x][$y] = $value;
               $x++;
     
           }
           $y++;
         }
     return $data;
    }
    
    Code (markup):
    you see what happens.
    (use mysql_query and mysql_fetch_array($your_handle, MYSQL_ASSOC) instead of my $db class)

    i show you something VERY COOL you can do with this function and your querys in 14 hours or so when im back from work :cool:
     
    falcondriver, Feb 15, 2007 IP
  6. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #6
    falcondriver, Feb 16, 2007 IP
  7. aco4god

    aco4god Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think you're able to get it in that format with a query. Should be very easy.
     
    aco4god, Feb 16, 2007 IP
  8. Jargonaut

    Jargonaut Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Using $$ is a way of creating a variable with the name of the value of the other variable. So $$random will create a variable whos name is $(whatever the value of $random is).
     
    Jargonaut, Feb 18, 2007 IP