【发布时间】:2017-02-14 14:24:11
【问题描述】:
我正在尝试通过类中的函数连接到 MySql。同样可以更轻松地完成,但这也是一种学习体验。代码都按预期工作,但是如果 MySql 查询失败,mysqli_error() 返回空白,mysqli_errno() 返回 0。我发现手动将数据输入 mysql 的错误,它是 db 中的一个太短的列,但是我不明白为什么 mysqli_error() 和 mysql_errno() 都没有报告错误。提前感谢您的帮助。
<?php
class FrmAddProduct {
protected function getDbConnection(){
return new mysqli("localhost", "root", "****", "test");
}
/**
*all variables are declares and assigned here
*
**/
function commitSignUp (){
$commitSignUp = "INSERT INTO Login ( `logTitle`,`logFirstName`, `logLastName`,`logLandLine`,) VALUE (\"$this->title\", \"$this->firstName\",\"$this->lastName\", \"$this->landLine\",)";
if ($this->getDbConnection()->query($commitSignUp)){
echo "The new product is added.";
return 1;
} else {
echo "Mysql reported this error description: ".mysqli_error($this->getDbConnection()."<br>";
echo "Mysql reported this error number: ".mysqli_errno($this->getDbConnection());
return 0;
}
【问题讨论】:
-
你正在混合 MySQLi OOP 风格和过程风格......使用其中一种,你不能混合使用。阅读手册中的更多内容:php.net/manual/en/mysqli.error.php