如何在扩展类中实现 mysqli?
我正在上传图像并将其存储在 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();
?>
最佳答案
您的代码中存在一些错误。
$mysqli
成员在抽象类中是私有(private)的。它不会被Check
类继承,因此它不存在于该类中。使其受到保护。访问类的成员总是需要前面的
$this->
,特别是本例中的$this->mysqli
。构造函数必须命名为
__construct
,前面有两个下划线。图像检查看起来有问题。 $_POST['image'] 确实包含您希望存储在数据库中的内容,但您还将它与整数值进行比较,并且如果它更大,则似乎会回显错误消息。虽然数据处理将起作用,例如您可以将 POST 数据中的字符串与整数进行比较,看起来您想要其他东西。
关于mysqli - undefined variable : mysqli - PHP OOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14696931/