简单的SQL语句/ PHP函数无法正常工作的问题

时间:2016-10-17 14:52:23

标签: php mysql

我有一个简单的函数可以写入我的数据库。这是我得到的错误。

这是我得到的错误

  

注意:尝试在第92行的/var/sites/q/quasisquest.uk/public_html/KeepScore/MySQLDao.php中获取非对象的属性致命错误:未捕获的异常'异常'在/var/sites/q/quasisquest.uk/public_html/KeepScore/MySQLDao.php:92堆栈跟踪:#0 /var/sites/q/quasisquest.uk/public_html/KeepScore/createCommunity.php(26):MySQLDao- > createCommunity(' radowns82 @ gmail ...',' YGHFYG',' Endcliffe')/ var / sites / q中抛出#1 {main}第92行的/quasisquest.uk/public_html/KeepScore/MySQLDao.php

这是调用它的初始PHP脚本:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$code = htmlentities($_POST["code"]);
$communityname = htmlentities($_POST["communityname"]);

$dao = new MySQLDao();
$dao -> openConnection();

$result = $dao -> createCommunity($email, $code, $communityname);

$dao->closeConnection();

?>

这是MySQLDao.php

<?php

class MySQLDao{

var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;

public function __construct(){
    $this->dbhost = Conn::$dbhost;
    $this->dbuser = Conn::$dbuser;
    $this->dbpass = Conn::$dbpass;
    $this->dbname = Conn::$dbname;
}

public function openConnection()
{
    $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
    if (mysqli_connect_errno())
        echo new Exception("Could not establish connection with database");

}

public function getConnection()
{
echo ("2");
    return $this->conn;
}

public function closeConnection()
    {
    if($this->conn!=null)
        $this->conn->close();
}

public function getUserDetails($email)
{
    $returnValue = array();
    $sql = "select * from users where user_email='".$email."'";

    $result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1)){
        $row = $result -> fetch_array(MYSQLI_ASSOC);
        if(!empty($row)){
            $returnValue = $row;
        }
    }
    return $returnValue;

}

public function getUserDetailsWithPassword($email, $userPassword)
{
    $returnValue = array();
    $sql = "select id, user_email, user_name from users where user_email = '".$email."' and user_password = '".$userPassword."'";

    $result = $this->conn->query($sql);
    if($result != null && (mysqli_num_rows($result) >= 1 )){
        $row = $result -> fetch_array(MYSQLI_ASSOC);
        if(!empty($row)){
            $returnValue = $row;
        }
    }
    return $returnValue;
}

public function registerUser($email, $password, $username)
{
    $sql = "insert into users set user_email=?,user_password=?,user_name=?";
    $statement = $this->conn->prepare($sql);

    if(!$statement)
        throw new Exception($statement->error);

    $statement->bind_param("sss", $email, $password, $username);
    $returnValue = $statement->execute();

    return $returnValue;
}

public function createCommunity($email, $code, $communityname)
{
    $sql = "insert into communities set email=?,code=?,communityname=?";
    $statement = $this->conn->prepare($sql);

if(!$statement){
        throw new Exception($statement->error);
}
    $statement->bind_param("sss", $email, $code, $communityname);
    $returnValue = $statement->execute();

    return $returnValue;
}


}

?>

那些社区&#39;表还有一个&#39; id&#39;我没有张贴的专栏(第1栏),因为我认为它会自动填充并增加......也许这就是我出错的地方?

1 个答案:

答案 0 :(得分:1)

如果连接首先失败,您需要知道原因,以便显示实际的数据库错误。第二,在没有连接数据库的情况下继续执行脚本没什么意义。

我可以建议您对openConnection()方法

进行此更改

此外,如果您认为MSYQLI代码中存在任何错误,这4行基本上可以确保您在开发过程中被告知任何错误,特别是如果您在关闭了ERROR REPORTING的实时服务器上进行开发

<?php
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


public function openConnection()
{
    $this->conn = new mysqli($this->dbhost, $this->dbuser, 
                             $this->dbpass, $this->dbname
                            );
    if ($mysqli->connect_error) {   
        echo 'Connect Error: ' . $mysqli->connect_errno . ' - '
        . $mysqli->connect_error;
        exit;
    }
}