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?