我正在使用 mysqli 和准备好的语句从 MySQL 表中选择大约 50 列。我的代码:
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();
}
}
我有两个问题,我也写到了源代码中。首先,当我有多个变量时,我不明白如何将结果绑定到变量?有没有办法将我的结果绑定到数组?我的第二个问题是在 while 循环内。我想将我的 50 个值添加到关联数组中,但是显示的示例不会在每次循环通过 while 循环时覆盖现有数组吗?