PHP中为什么第二行显示错误Class 'MyDB' not found in。。困扰多天,求大神帮助啊!
文件在根目录的,但是还是不行。。。product_class.php<?phpclassProductModleextendsMyDB{publicfunctionadd...
文件在根目录的,但是还是不行。。。
product_class.php
<?php
class ProductModle extends MyDB{
public function addProduct($product){
$query="INSERT INTO product(name,price,description)values(?,?,?)";
$stmt=$this->mysqli->prepare($query);
$stmt->bind_param('sds',$name,$price,$description);
$name=$product->getName();
$price=$product->getSrcPrice();
//$image=$product->getimage();
$description=$product->getDescription();
$stmt->execute();
if($stmt->affected_rows!=1){
$this->printerror("数据插入失败:".$stmt->error);
return false;
}else {
return $this->mysqli->insert_id;
}
}
}
?>
MyDB_class.php
<?php
class MyDB{
protected $mysqli;
protected $showError;
public function __construct($configFile="password.php",$showError=TRUE){
require_once ($configFile);
$this->mysqli=new mysqli($dbhost,$dbuser,$dbpasswd,$dbname);
if (mysqli_connect_errno()){
$this->printError("链接失败,原因是:".mysqli_connect_error());
$this->mysqli=false;
exit();
}
$this->showError=$showError;
}
protected function printError($errorMessage){
if($this->showError){
echo'<p><font color="red">'.htmlspecialchars($errorMessage).'</font></p>';
}
}
public function close(){
if($this->mysqli)
$this->mysqli->close();
$this->mysqli=false;
}
public function __destruct(){
$this->close();
}
}
?> 展开
product_class.php
<?php
class ProductModle extends MyDB{
public function addProduct($product){
$query="INSERT INTO product(name,price,description)values(?,?,?)";
$stmt=$this->mysqli->prepare($query);
$stmt->bind_param('sds',$name,$price,$description);
$name=$product->getName();
$price=$product->getSrcPrice();
//$image=$product->getimage();
$description=$product->getDescription();
$stmt->execute();
if($stmt->affected_rows!=1){
$this->printerror("数据插入失败:".$stmt->error);
return false;
}else {
return $this->mysqli->insert_id;
}
}
}
?>
MyDB_class.php
<?php
class MyDB{
protected $mysqli;
protected $showError;
public function __construct($configFile="password.php",$showError=TRUE){
require_once ($configFile);
$this->mysqli=new mysqli($dbhost,$dbuser,$dbpasswd,$dbname);
if (mysqli_connect_errno()){
$this->printError("链接失败,原因是:".mysqli_connect_error());
$this->mysqli=false;
exit();
}
$this->showError=$showError;
}
protected function printError($errorMessage){
if($this->showError){
echo'<p><font color="red">'.htmlspecialchars($errorMessage).'</font></p>';
}
}
public function close(){
if($this->mysqli)
$this->mysqli->close();
$this->mysqli=false;
}
public function __destruct(){
$this->close();
}
}
?> 展开
3个回答
展开全部
<?php
include_once "MyDB_class.php";
class ProductModle extends MyDB{
public function addProduct($product){
$query="INSERT INTO product(name,price,description)values(?,?,?)";
$stmt=$this->mysqli->prepare($query);
$stmt->bind_param('sds',$name,$price,$description);
$name=$product->getName();
$price=$product->getSrcPrice();
//$image=$product->getimage();
$description=$product->getDescription();
$stmt->execute();
if($stmt->affected_rows!=1){
$this->printerror("数据插入失败:".$stmt->error);
return false;
}else {
return $this->mysqli->insert_id;
}
}
}
?>
展开全部
放在哪个目录不是关键。因为php的类的引入不是像java那样。它是靠require或include来实现的。如果就这两个文件的话,你那个继承类里要include父类。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不引入就继承是不可能的。得先用require或者include引入类之后才能进行相关操作。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询