/    Sign up×
Community /Pin to ProfileBookmark

Debug Help PHP /SQL Call member function*non-object in

I have taken over this code/website from a previous developer and am just getting more in depth with PHP and SQL. So I need some help, after much searching I have not found a similar issue.

I am receiving this error –
Call to a member function [B]query()[/B] on a non-object in …[B]inc_sql.php on line 27[/B]

The code snippet-
<?PHP
//global $dbc;
$dbc = new DBI;
if (realpath(__FILE__) == realpath($_SERVER[‘SCRIPT_FILENAME’])) exit(‘This file can not be accessed directly…’);

class DBI
{
public $dbconn, $nfreed;
private $result, $threads;
[INDENT][INDENT]function __construct()
{
if(basename($_SERVER[‘SCRIPT_NAME’])==”message.php” && $_GET[‘error’]) return “”;
$this->nfreed=false;
$sqlhost = “IP”;
//$sqlhost = “localhost”;
@$this->dbconn = new mysqli($sqlhost, “user”, “password”, “database”);[/INDENT][/INDENT]

[INDENT][INDENT]if(mysqli_connect_errno()) header(“Location: message.php?error=sqlserver”);
//die(“Critical Error: “.mysqli_connect_error());
}
[/INDENT][/INDENT]
[INDENT][INDENT]function query($sql, $flag = MYSQLI_STORE_RESULT)
{
//echo(“<p>”.$sql.”</p>”);
if($this->nfreed) {$this->free();}
[B] if(!$this->result = $this->dbconn->query($sql)) header(“Location: message.php?error=sqlcmd&errno=”.$this->dbconn->errno);[/B]
//die(“<pre>Mysql Error: “.$this->dbconn->error.”</pre>”);
$this->nfreed=true;
return $result;
}[/INDENT][/INDENT]
[INDENT][INDENT] function get_threads() {
global $threads;
if($threads==””){
ereg (“Threads: (…)”, $this->dbconn->stat(), $regs);
$threads = trim($regs[1]);
}
return $threads;
} [/INDENT][/INDENT]

Any help is greatly appreciated.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderNov 02.2007 — [code=php]
@$this->dbconn = new mysqli($sqlhost, "user", "password", "database");
[/code]


That is the cause of the issue, remove the @ from the front of the line and re-run the script and you should see the correct error message.

Placing @ in front of a function will suppress any error message, so by removing it you are allowing the error to be displayed.


[code=php]
if(mysqli_connect_errno()) header("Location: message.php?error=sqlserver");
//die("Critical Error: ".mysqli_connect_error());
[/code]


Replace the above with..

[code=php]
if(mysqli_connect_errno())// header("Location: message.php?error=sqlserver");
die("Critical Error: ".mysqli_connect_error());
[/code]


While you debug..
Copy linkTweet thisAlerts:
@hhawkinsauthorNov 02.2007 — I did try that as well as trying to uncomment the other die command. But I received the same error.

My original thought was that it has something to do with global variables or classes, or the ! in that line prior to the this.

Thanks for your help.
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — [code=php]$this->dbconn->query($sql)[/code][/QUOTE]What is this? What is it supposed to do?
Copy linkTweet thisAlerts:
@hhawkinsauthorNov 02.2007 — I am really not sure, but I was wondering why that variable was not declared anywhere - this is the rest of the code after what I included.

[code=php] function odata($sql) {
static $res, $oldsql;
if($sql != $oldsql) {$res = $this->query($sql);$oldsql=$sql;}
return $this->result->fetch_object();
}
function sodata($sql) {
$res = $this->dbconn->query($sql);
return $res->fetch_object();
}
function adata($sql) {
static $res, $oldsql;
if($sql != $oldsql) {$res = $this->query($sql);$oldsql=$sql;}
return $this->result->fetch_assoc();
}
public function free() {
$this->nfreed=false;
$this->result->free();
}
public function quote_smart($value){
if (get_magic_quotes_gpc()) $value = stripslashes($value);
if($value == '') $value = 'NULL';
else if (!is_numeric($value) || $value[0] == '0') $value = "'" . $this->dbconn->real_escape_string($value) . "'"; //Quote if not integer
return $value;
}

};

// mysqli_real_escape_string($str);
?>[/code]
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — No point posting more code. Sort out the syntax error indicated above and try again. Also why would you be trying to call your [I]query [/I]method recursively anyway.
Copy linkTweet thisAlerts:
@hhawkinsauthorNov 05.2007 — Okay $sql is a query that determines the page content based on a database.
×

Success!

Help @hhawkins spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...