连接数据库失败

时间:2016-10-06 23:01:18

标签: php mysql database connection

我似乎无法弄清楚我的数据库或与之连接的问题。当我尝试运行它时,我收到此错误。

  

注意:尝试在第54行的/Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php中获取非对象的属性

  

致命错误:未捕获的异常'异常'在/Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php:53堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/scripts/registerUser.php(41):MySQLDAO-> registerUser( '电子邮件',' aske',' meyer',' 1bf528e7f15d11c ...',' KO \ x8E \ xD0 \第53行/Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php中引发的xCE / \ xBD \ xACK \ xD1d \ x18 \ x9A \ x07 \ xE1 ...')#main {main}

连接文件:

<?php class Conn { public static $dbhost = "localhost";
        public static $dbuser = "root";
        public static $dbpass = "";
        public static $dbname = "app";} ?>

MySQL文件:

public function registerUser($email, $first_name, $last_name, $password, $salt)
{ 
    $sql = "insert into users set email=?, first_name=?, last_name=?, user_password=?, salt=?";
    $statement = $this->conn->prepare($sql);
    if (!$statement)
      throw new Exception($statement->error);
    $statement->bind_param("sssss", $email, $first_name, $last_name, $password, $salt);
    $returnValue = $statement->execute();
    return $returnValue;  
}   

这是第54行。if (!$statement) throw new Exception($statement->error);

1 个答案:

答案 0 :(得分:0)

if()块在$statement = FALSE时运行,因此$statement->error可能不对。 error属性位于$this->conn,因此应为

if (!$statement) {
    throw new Exception($this->conn->error);
}

然后错误消息将显示您的SQL问题。

相关问题