如何在扩展类中实现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