如何在擴展類中實現mysqli?未定義變量:mysqli - PHP OOP
我的圖片上傳並存儲在MySQL數據庫中,但我得到這個錯誤:
Notice: Undefined variable: mysqli in ...ecc/ecc/ on line 33
Fatal error: Call to a member function query() on a non-object in ...ecc/ecc/ on line 33
這裏是我的測試代碼:
<?php
interface ICheckImage {
public function checkImage();
public function sendImage();
}
abstract class ACheckImage implements ICheckImage {
public $image;
private $mysqli;
public function _construct(){
$this->image = $_POST['image'];
$this->mysqli = new mysqli('localhost','test','test','test');
}
}
class Check extends ACheckImage {
public function checkImage() {
if($this->image > 102400) {
echo "File troppo grande";
}
}
public function sendImage() {
//This is the line 33 give me the error
if ($mysqli->query("INSERT INTO images (image) VALUES ('$this->image')")) {
echo "Upload avvenuto  ";
} else {
echo "Errore  " . $mysqli->error;
}
}
}
$form = new Check();
$form->checkImage();
$form->sendImage();
?>
...... OP還需要直接引用'$ this-> mysqli-> query()',而不是'$ mysqli'(局部變量)。 – MrWhite
謝謝,第一個錯誤通過:) ,但現在給我同樣的錯誤 致命錯誤:調用一個成員函數查詢()在非線對..ecc/.. ecc /在線33 – starbuck
@ user2041211:你似乎沒有從擴展類調用父構造函數(建立數據庫連接)? (該錯誤表明您沒有數據庫連接。) – MrWhite