我已经实现了这个,它在函数中返回一个数组,如下所示:
public function get_events()
{
$query = "SELECT * FROM events;";
if($stmt = mysqli_prepare($this->Database, $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);
}
echo '<pre>';
print_r($myArray);
}
/* CALL */
$Events = new Events();
$Events->get_events();
数组按照我的预期显示。
然而,当我将其设置为&#39;返回$ myArray&#39;而不是&#39; print_r&#39;并再次调用该函数返回:
/* CALL */
$Events = new Events();
$Events->get_events();
echo '<pre>';
print_r($Events);
返回 - &gt;
Events Object
(
[Database] => mysqli Object
(
[affected_rows] => -1
[client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: f373ea5dd5538761406a8022a4b8a374418b240e $
[client_version] => 50011
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[error_list] => Array
(
)
[field_count] => 10
[host_info] => localhost via TCP/IP
[info] =>
[insert_id] => 0
[server_info] => 5.6.21
[server_version] => 50621
[stat] => Uptime: 100448 Threads: 1 Questions: 1057 Slow queries: 0 Opens: 124 Flush tables: 1 Open tables: 83 Queries per second avg: 0.010
[sqlstate] => 00000
[protocol_version] => 10
[thread_id] => 186
[warning_count] => 0
)
我希望能够返回一个数组,以便我可以在多个实例中使用该查询。而不是在函数内回显html并且必须在其他地方为相同的数据重复相同的查询。
答案 0 :(得分:5)
/* CALL */
$Events = new Events();
$myArray = $Events->get_events();
echo '<pre>';
print_r($myArray);
您必须将输出分配给变量。