Jump to content

[SOLVED] building class function to retrieve mysql rows to array


severndigital

Recommended Posts

ok .. i am working on a class function that will take the sql statement and loop through it to return an array.

 

i'm stuck in the middle .. if anyone can help, it would be great.

 

here's what i have


class database
{

  public function returnArray($sql)
  {

      $pull = mysql_query($sql) or die ('Return Array Failed: ' . mysql_error());
      $returnArray = array();
      //this is where i get stuck 
     while($r = mysql_fetch_array($pull){
           $itemArray = array();
      
           //how do i put the $r items into the array $itemArray ???

           $returnArray[] = $itemArray;
     }

    return $returnArray;
}

 

 

i tried using this

 

 

while($r = mysql_fetch_array($pull){
           $itemArray = array();
      
           for($i = 0;$i <count($r); $i++){
                  $itemArray[] = $r[$i];
           } 

           $returnArray[] = $itemArray;
}

 

but i get tons of invaild resource id errors

 

can anyone help??

 

Thanks,

-C

 

 

 

ok that kind of worked ..but it's return twice as much information .. what can i do to ONLY return the array with either a number or the fieldname

 

here is what gets returned:

Array
(
    [0] => Array
        (
            [0] => 1
            [elementId] => 1
            [1] => 1
            [userId] => 1
            [2] => 1
            [designId] => 1
            [3] => text
            [elementType] => text
            [4] => 0
            [xCord] => 0
            [5] => 0
            [yCord] => 0
            [6] => New Text
            [elementValue] => New Text
        )

    [1] => Array
        (
            [0] => 2
            [elementId] => 2
            [1] => 1
            [userId] => 1
            [2] => 1
            [designId] => 1
            [3] => text
            [elementType] => text
            [4] => 0
            [xCord] => 0
            [5] => 0
            [yCord] => 0
            [6] => New Text
            [elementValue] => New Text
        )

)

 

i cannot preform a for loop correctly on the information because it returns twice as many when i do the count()

 

thanks,

-C

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.