我有一个PHP类我在哪里initilizing在构造从数据库retieved数组:如何通过PHP中获取查询循环使用的MySQL
class TableClass{
public function __construct($con, $id){
$this->con = $con;
$query = mysqli_query($this->con, "SELECT * FROM table1 WHERE id='$id'");
$this->table_result= mysqli_fetch_array($query);
}
}
我有,我想用数组不同的功能而不必再次获取。我只需要循环查看结果并进行一些计算。 我试过的语法如下:
public function getNumberOfComments(){
for ($i = 0; $i < count($this->table_result); $i++) {
$comment= $this->table_result[$i]['comment'];
}
}
我得到一个错误:Illegal string offset "comment"
。
什么是正确的语法?
您只从结果集中提取一行,那么为什么使用'for'循环呢?简单的'$ comment = $ this-> table_result ['comment'];'会做得很好。 –
首先尝试'print_r'结果'$ this-> table_result'来查看究竟究竟是什么被提取 –