gpt4 book ai didi

PHP 返回数组

转载 作者:行者123 更新时间:2023-11-29 17:01:37 25 4
gpt4 key购买 nike

我已经开始学习 mvc 模式并得到了简单的例子。但我无法通过返回来显示来自 mysql 的数据。

此代码中有 3 个示例(现在试图弄清楚如何以及为什么。)

    public function getPostsList()
{
// get data from database

$stmt = $this->conn->prepare("SELECT * FROM visitors LIMIT 5");
$stmt->execute();
$result = $stmt->fetchAll();

// Checking if my array works fine. And it works. (Check result 1.)

$visitors = array();
foreach($result as $user) {
$visitors[] = $user;
}

print_r($visitors);

// Trying to return my array (Don't work. See result 2.)

return $visitors;

// This is how data are shown in example (Line one ("Jungle Book" works. See result 3.)

return array(
"Jungle Book" => new Book($visitors[3]['id'], $visitors[3]['ip'], $visitors[3]['date']),
"Moonwalker" => new Book("Moonwalker", "J. Walker", ""),
"PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")
);


}

结果1:

Array ( [0] => Array ( [id] => 1 [0] => 1 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 16:52:27 [2] => 2018-06-01 16:52:27 ) [1] => Array ( [id] => 2 [0] => 2 [ip] => 66.249.92.20 [1] => 66.249.92.20 [date] => 2018-06-01 16:52:52 [2] => 2018-06-01 16:52:52 ) [2] => Array ( [id] => 3 [0] => 3 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 17:00:14 [2] => 2018-06-01 17:00:14 ) [3] => Array ( [id] => 4 [0] => 4 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 17:01:46 [2] => 2018-06-01 17:01:46 ) [4] => Array ( [id] => 5 [0] => 5 [ip] => 217.166.253.194 [1] => 217.166.253.194 [date] => 2018-06-01 18:31:47 [2] => 2018-06-01 18:31:47 ) )

结果2:

Notice: Trying to get property 'id' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'id' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'ip' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'date' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'id' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'id' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'ip' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

Notice: Trying to get property 'date' of non-object in D:\xampp\htdocs\view\booklist.php on line 12

结果3:

Title Author Description

4 217.166.253.194 2018-06-01 17:01:46

Moonwalker J. Walker

PHP for Dummies Some Smart Guy

结果 4 (var_dump)

array(5) { [0]=> array(6) { ["id"]=> string(1) "1" [0]=> string(1) "1" ["ip"]=> string(15) "217.166.253.194" [1]=> string(15) "217.166.253.194" ["date"]=> string(19) "2018-06-01 16:52:27" [2]=> string(19) "2018-06-01 16:52:27" } [1]=> array(6) { ["id"]=> string(1) "2" [0]=> string(1) "2" ["ip"]=> string(12) "66.249.92.20" [1]=> string(12) "66.249.92.20" ["date"]=> string(19) "2018-06-01 16:52:52" [2]=> string(19) "2018-06-01 16:52:52" } [2]=> array(6) { ["id"]=> string(1) "3" [0]=> string(1) "3" ["ip"]=> string(15) "217.166.253.194" [1]=> string(15) "217.166.253.194" ["date"]=> string(19) "2018-06-01 17:00:14" [2]=> string(19) "2018-06-01 17:00:14" } [3]=> array(6) { ["id"]=> string(1) "4" [0]=> string(1) "4" ["ip"]=> string(15) "217.166.253.194" [1]=> string(15) "217.166.253.194" ["date"]=> string(19) "2018-06-01 17:01:46" [2]=> string(19) "2018-06-01 17:01:46" } [4]=> array(6) { ["id"]=> string(1) "5" [0]=> string(1) "5" ["ip"]=> string(15) "217.166.253.194" [1]=> string(15) "217.166.253.194" ["date"]=> string(19) "2018-06-01 18:31:47" [2]=> string(19) "2018-06-01 18:31:47" } }

书单.php

    <?php

foreach ($books as $title => $book)
{
echo '<tr><td><a href="index.php?book='.$book->id.'">'.$book->id.'</a></td><td>'.$book->ip.'</td><td>'.$book->date.'</td></tr>';
}

?>

图书类(class)

    <?php

require_once('controller/Config.php');

class Book {
public $id;
public $ip;
public $date;

public function __construct($id, $ip, $date)
{
$this->id = $id;
$this->ip = $ip;
$this->date = $date;
}
}

?>

最佳答案

在 booklist.php 中,第 1 行。 12,您正在尝试使用箭头访问属性 id,它不是对象,而是数组,因此请尝试使用 ['id'] 而不是 -> 访问它id。这可能会有所帮助。

关于PHP 返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52186359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com