Jump to content

[SOLVED] Call to a member function query() on a non-object?


matthewhaworth

Recommended Posts

<?php
class db
{
private $_db;
    private $_report;
    private $_querycount = 0;
    private $_tempquery;

    function __constructor($report, $db, $user, $pass, $host = 'localhost')
    {
        $this->_db = new mysqli($host, $user, $pass, $db);
        if ($this->_db->connect_errno) {
            $this->_report->addError('Database connection failed: ' . $this->_db->connect_errno);
        } else {
            $this->_report->addNote('Database connection successful.');
        }
    }

    function query($sql)
    {
        if ($this->_tempquery = $this->_db->query($_db, $sql)) {
            $this->_report->addNote('Query successful.');
            $this->_querycount++;
            return $qry;
        } else {
            $this->_report->addError('Query unsuccessful: <br />' . $sql . '<br />' . $this->_db->connect_error);
        }
    }

    function numrows($qry = NULL)
    {
    	if($qry)
    	{
    		$qry = $this->_tempquery;
    	}
        return $qry->num_rows;
    }

    function fetch($qry = NULL)
    {
    	if($qry)
    	{
    		$qry = $this->_tempquery;
    	}
        return $qry->fetch_assoc();
    }

    function multifetch($qry = NULL)
    {
    	if($qry)
    	{
    		$qry = $this->_tempquery;
    	}
    	
        $rows = array();
        while ($row = $qry->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }
}
?>

 

Fatal error: Call to a member function query() on a non-object in C:\apache2triad\htdocs\forum\db.class.php on line 21

 

if ($this->_tempquery = $this->_db->query($_db, $sql)) {

 

That is the line in question if you can't be bothered counting lol.

First of all, where is $_db coming from?

if ($this->_tempquery = $this->_db->query($_db, $sql)) {

 

Do you mean $this->_db? Also, can I see how you try to call this function?

 

Very good point, it should be $this->$_db.

 

Edit: No, it shouldn't lol, it doesn't need a link parameter, s'me being stupid.

 

The problem seems to be fixed now.  However, I'm still getting nothing but white (with errors turned on : P) but that's another problem I should be able to handle myself.

 

Thank you : ].

Yeah, I just realized that too.  You just need the $sql, correct?

 

Aye.  I probably read the php.net function definition for the procedural one, without paying much attention.  I think they've done a really poor job with the mysqli documentation.

I retract my previous statement, it's still not working with the same error message.

 

<?php
//connect to server
require("inc.php");

//gather the topics
$get_topics_sql = "SELECT topic_id, topic_title, DATE_FORMAT(topic_create_time,  '%b %e %Y at %r') aS fmt_topic_create_time, topic_owner FROM forum_topics ORDER BY topic_create_time DESC";
$get_topics_res = $db->query($get_topics_sql);
?>

 

 

Here is inc.php

 

<?php

require("report.class.php");
require("db.class.php");

$report = new report();
$db = new db($report, "forum", "root", "passwordlol");

?>

Did that fix anything?

 

Nope : [.

 

Edit: If anybody else read my other thread and has realised that I have corrected the error I had with default parameters incorrectly.. don't worry, I realise it should be "if ($qry == NULL)" now lol.

Here is: "db.class.php"

<?php
class db
{
private $_db;
    private $_report;
    private $_querycount = 0;
    private $_tempquery;

    function __constructor($report, $db, $user, $pass, $host = 'localhost')
    {
        $this->_db = new mysqli($host, $user, $pass, $db);
        $this->_report = $report;
        if ($this->_db->connect_errno) {
            $this->_report->addError('Database connection failed: ' . $this->_db->connect_errno);
        } else {
            $this->_report->addNote('Database connection successful.');
        }
    }

    function query($sql)
    {
        if ($this->_tempquery = $this->_db->query($sql)) {
            $this->_report->addNote('Query successful.');
            $this->_querycount++;
            return $qry;
        } else {
            $this->_report->addError('Query unsuccessful: <br />' . $sql . '<br />' . $this->_db->connect_error);
            
        }
    }

    function numrows($qry = NULL)
    {
    	if($qry == NULL)
    	{
    		$qry = $this->_tempquery;
    	}
    	
        return $qry->num_rows;
    }

    function fetch($qry = NULL)
    {
    	if($qry == NULL)
    	{
    		$qry = $this->_tempquery;
    	}
    	
        return $qry->fetch_assoc();
    }

    function multifetch($qry = NULL)
    {
    	if($qry == NULL)
    	{
    		$qry = $this->_tempquery;
    	}
    	
        $rows = array();
        while ($row = $qry->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }
}
?>

 

Here is: "report.class.php"

<?php
class report 
{
private $_userinfo = array();
private $_errors = array();
private $_notes = array();

public function addError($err)
{
	$this->_errors[] = $err;
}

public function addNote($note)
{
	$this->_notes[] = $note;
}

public function debug()
{
	$t = "<div name='debug'>";
	foreach($this->_userinfo as $userinfo)
	{
	$t .= $userinfo;
	$t .= "<br />";
	}
	foreach($this->_errors as $errors)
	{
	$t .= $errors;
	$t .= "<br />";
	}
	foreach($this->_notes as $notes)
	$t .= $notes;
	$t .= "</div>";		

	return $t;
}

}
?>

 

Here is: "inc.php"

<?php

require("report.class.php");
require("db.class.php");

$report = new report();
$db = new db($report, "forum", "root", "passwordlol");

?>

 

Thank you a LOT for your help so far : ].

You might kill yourself, but you named the constructor __constructor instead of __construct.

 

Omg. No.

Thanks a lot, you've been great.

Oh it's embarrassing. have to rush off now anyway lol.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.