0

I have a simple function to write into my database. This is the error I am getting.

This is the error I am getting

Notice: Trying to get property of non-object in /var/sites/q/quasisquest.uk/public_html/KeepScore/MySQLDao.php on line 92 Fatal error: Uncaught exception 'Exception' in /var/sites/q/quasisquest.uk/public_html/KeepScore/MySQLDao.php:92 Stack trace: #0 /var/sites/q/quasisquest.uk/public_html/KeepScore/createCommunity.php(26): MySQLDao->createCommunity('radowns82@gmail...', 'YGHFYG', 'Endcliffe') #1 {main} thrown in /var/sites/q/quasisquest.uk/public_html/KeepScore/MySQLDao.php on line 92

This is the initial PHP script that calls it:

<?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();

?>

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


}

?>

That 'communities' table also has an 'id' column (1st column) which I am not posting to as I thought it would auto-populate and increment... maybe this is where I am going wrong?

6
  • Maybe your '$this->conn is null. Check that or show us more code. Commented Oct 17, 2016 at 14:58
  • 1
    You're checking if $statement is false, so it wouldn't be an object. Check for mysqli_error on your connection instead. Commented Oct 17, 2016 at 15:01
  • 1
    Post the full MySQLDao class Commented Oct 17, 2016 at 15:02
  • the connection $this->conn definitely works fine, as other functions are contained within that MySQLDao.php file. Commented Oct 17, 2016 at 15:03
  • Use throw new Exception($this->conn->error); Commented Oct 17, 2016 at 15:06

1 Answer 1

1

If the connection fails first you need to know why so show the actual database error. and second, there is very little point in continuing the scripts execution without a connection to the database.

So can I suggest this change to your openConnection() method

Also if you think there is any chance of something wrong in the MSYQLI code these 4 lines will basically ensure you get told about any errors, while you are developing, specially if you are developing on a live server with ERROR REPORTING turned off.

<?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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.