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

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.