dongqi9125 2016-10-06 23:01
浏览 55

连接数据库失败

I can't seem to figure out the problem with my database or the connection to it. I get this error when I try to run it.

Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php on line 54

AND

Fatal error: Uncaught exception 'Exception' in /Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php:53 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/scripts/registerUser.php(41): MySQLDAO->registerUser('email', 'aske', 'meyer', '1bf528e7f15d11c...', 'KO\x8E\xD0\xCE/\xBD\xACK\xD1d\x18\x9A\x07\xE1...') #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/swiftappandmysql/db/MySQLDAO.php on line 53

Connection file:

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

MySQL file:

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;  
}   

This is line 54. if (!$statement) throw new Exception($statement->error);

  • 写回答

1条回答 默认 最新

  • doqo89924 2016-10-06 23:13
    关注

    The if() block runs when $statement = FALSE, so $statement->error can't possibly be right. The error property is in $this->conn, so it should be

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

    Then the error message will show the problem with your SQL.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分