PHP Object and MySQL - Call to a member function query() on a non-object [duplicate]

StackOverflow https://stackoverflow.com/questions/23587325

  •  19-07-2023
  •  | 
  •  

Question

I am sure the error is staring me in the face, but I will still ask it, as I have spent several hours already trying to figure it out. I don't know why, but the query causes HTML to stop rendering entirely. Not just within the PHP tag, but all HTML. Below is a very trimmed down version of my code, just enough to get the error. What should be displayed is the number 1 followed by a var_dump, then 2, var_dump, etc... but all I get is 1, then the error.

<?php
class Calendar {
 private $DBConnect = NULL;

    function __construct() {
        $ErrorMsgs = array();
        $DBConnect = new mysqli("localhost", "user", "pass", "db");
        if ($DBConnect->connect_error)
            $ErrorMsgs[] = "The database server is not available. Connect Error is " . $mysqli->connect_errno . " " . $mysqli->connect_error . ".";
        $this->DBconnect = $DBConnect;  
    }

    public function getMonthlyCalendar($Year, $Month) {
        for ($i = 1; $i <= 31; ++$i) {
            echo $i;
            $SQLstring = "SELECT EventID, Title FROM event_calendar WHERE EventDate='2014-05-$i'";
            $QueryResult = $this->DBConnect->query($SQLstring);
            var_dump($QueryResult);
        }
    }
}

$Calendar = new Calendar();
$Calendar->getMonthlyCalendar(2014, 5);
?>
Was it helpful?

Solution

You have a typo in your code:

you declared

$this->DBconnect = $DBConnect;  

but then you used

$QueryResult = $this->DBConnect->query($SQLstring);

with C in DBConnect uppercase.

OTHER TIPS

that error message is usually an error in the query. if you are using phpmyadmin copy your query, and run it in your phpmyadmin query box and see if it returns results.if your query works then you know is an error in your connection script

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top