我在一个函数中创建了一个数据库查询,并在视图中返回一个数组,我可以在以后的多个实例中使用它。
然而,当我调用该函数时,它返回一个Object(“Events Object([Database] => mysqli Object”)而不是我计划的数组。
我知道查询的效果就像'print_r($ events)'而不是函数本身中的'return $ events'我可以按预期看到所有结果信息。
public function get_events()
{
$stmt = $this->Database->prepare("SELECT 'id', `title`, `event_desc`, `date`, `time`, `location`, `spots`, `event_city`, `event_spots_available`, `event_series` FROM `events` ORDER BY `date` ASC");
$stmt->execute();
$events = array();
/* bind variables to prepared statement */
$stmt->bind_result($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8, $col9, $col10);
/* fetch values */
while ($stmt->fetch())
{
$events[] = array(
'event_id' => $col1,
'event_title' => $col2,
'event_desc' => $col3,
'event_date' => $col4,
'event_time' => $col5,
'event_location' => $col6,
'event_spots' => $col7,
'event_city' => $col8,
'event_spots_available' => $col9,
'event_series' => $col10
);
}
$stmt->close();
return $events;
}
}
请告知我的错误?
答案 0 :(得分:0)
如果您使用预准备语句并希望将结果作为数组,我建议这样:
$query= "SELECT * FROM events;";
if($stmt = mysqli_prepare($connection, $query))
{
mysqli_stmt_bind_param($stmt, "s", $param);
mysqli_stmt_execute($stmt);
$resultObject = mysqli_stmt_get_result($stmt);
mysqli_stmt_close($stmt);
}
$myArray = array();
$size = mysqli_num_rows($resultObject);
for($i = 0; $i < $size; $i++)
{
$myArray[$i] = mysqli_fetch_array($resultObject, MYSQLI_ASSOC);
}