我有一個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'來查看究竟究竟是什麼被提取 –