1

I am selecting about 50 columns from a MySQL table with mysqli and prepared statements. My code:

if ($stmt = $mysqli->prepare("SELECT ".explode(",",$some_arr)." FROM some_table WHERE userid=?"))
{       
    $stmt->bind_param("i", $user->get_userid());                        
    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;

    // Do I have to add 50 variables here? What if some_arr contains less than 50?
    $stmt->bind_result($col1, $col2);

    if($num_rows > 0)
    {                   
        while($stmt->fetch())
        {
             // wouldn´t this create a new array within each while loop?
             $output_arr[] = array("key1" => $col1, "key2" => $col2);
        }   

        $stmt->close();

    }
}

I have 2 questions which I also wrote into the source code. First I dont understand how to bind my result to variables when I have more than just a few? Is there a way to bind my result to an array? My second question is inside the while loop. I want to add my 50 values to an associative array, but wouldn´t the shown example overwrite the existing array everytime it is looping through the while loop?

Chris
  • 6,093
  • 11
  • 42
  • 55
  • I think this is what you are trying to do: http://stackoverflow.com/questions/280798/is-there-a-way-to-bind-an-array-to-mysqli-prepare – David Jun 18 '12 at 19:04
  • As far as I understood that question was about how to bind multiple variables to my SQL statement using bind_param(); But I would like to bind the result to an array. – Chris Jun 18 '12 at 20:41

0 Answers0