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.

Link to comment
Share on other sites

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 : ].

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

?>

Link to comment
Share on other sites

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 : ].

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.