我正在写这个评论课:
class Comment {
public $id;
public $post_id;
public $name;
public $email;
public $website;
public $body;
public $date;
public $ip_address;
public $status;
function __construct($id) {
global $db;
$resc = $db->query("SELECT * FROM blog_comments WHERE id='$id' LIMIT 1");
while($row = $db->fetch_assoc($resc)) {
while ($comment = current($row)) {
$key = key($row);
$this->$key = $comment{$key};
next($row);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是在数据库中运行时构造函数内部的查询将返回的内容:
查询结果http://17webshop.com/wp-content/uploads/2009/10/Picture-2.png
但是当我运行它时,这就是print_r(new Comment(1)); 吐出:
Comment Object
(
[id] => 1
[post_id] => 1
[name] => J
[email] => j
[website] => h
[body] => b
[date] => 1
[ip_address] => :
[status] => 1
)
Run Code Online (Sandbox Code Playgroud)
任何想法为什么我只获得每个领域的第一个角色?
谢谢.
你要
$comment[$key]
Run Code Online (Sandbox Code Playgroud)
$ comment {$ key}将为您提供字符串的第n个字符.由于$ key本身是一个字符串,因此PHP将其转换为整数0并获得第一个字符.