Jump to content

[SOLVED] Freakin Error reporting errors! Trying to catch MySql errors inside class...


Recommended Posts

Ok, i have a mysql class, and it's worked fine for months now, it's not the problem. I just created a Bug Reporting class, and it works... when implemented correctly. When something doesn't go as planned, i call the bug class. Well, i was trying to include the Mysql Errors in the bug reports, but i'm not able to freakin catch them!

 

If i say mysql_query($blah) or die (mysql_error());

It's fine, it KILLS the script, and give the error. BUT, if i do this:

 

function Q($sql){
	$query = mysql_query($sql) or $this->bugout($sql);
	return $query;
}

I get NOTHING;

 

I've also tried this:

function Q($sql){
	$query = mysql_query($sql);
	if ($query == false){
		$B =  new Bug("There was an error in a query. Details: " . mysql_error());
	}else{
		return $query;
	}
}

and this (con being the connection object.)

function Q($sql){
	$query = mysql_query($sql);
	if ($query == false){
		$B =  new Bug("There was an error in a query. Details: " . mysql_error($this->con));
	}else{
		return $query;
	}
}

and this

function Q($sql){
	$query = mysql_query($sql);
	if ($query == false){
		$B =  new Bug("There was an error in a query. Details: " . $sql);
	}else{
		return $query;
	}
}

And i'm getting NOTHING in return. I get the plain text section of the error, but not the variables or the mysql_error. I could eat glass right now!

 

Thanx

I even tried this just now (completely different approach) to no avail


function BugOut($args){
	$this->O();
	$this->Q("insert into BugReports (host,uri,uid,ctime,description)values('{$_SERVER['HTTP_HOST']}','{$_SERVER['REQUEST_URI']}','{$_COOKIE['uid']}','".date("Y-m-d H:i:s")."','$args')");
	$this->C();
	return true;
}



function Q($sql){
	$query = mysql_query($sql);
	if ($query == false){
		$this->BugOut(mysql_error());
		return false;
	}else{
		return $query;
	}
}


I've narrowed it down to the Bug Class. I'm having a problem on autoloading AND extending the Mysql class. How the hell am i to call the parent constructor when extending and autoloading, if the Bug class is being called from a page that has also already included the Mysql class via autoload?

<?php
class Bug extends MySql{
private $host;
private $uri;
private $uid;
private $desc;
private $ctime;

function __construct($description){
	$this->host= $_SERVER['HTTP_HOST'];
	$this->uri = $_SERVER['REQUEST_URI'];
	if (isset($_COOKIE['uid'])){
		$this->uid = stripslashes(str_replace("'","''",$_COOKIE['uid']));
	}else{
		$this->uid = "Guest, or Not Logged In";
	}
	$this->desc= $description;
	$this->ctime=date("Y-m-d H:i:s");
	parent::__construct;
	parent:();
	parent::Q("insert into BugReports (host,uri,uid,ctime,description)values('{$this->host}','{$this->uri}','{$this->uid}','{$this->ctime}','{$this->desc}')");
	echo "<h1>There was a bug, but it was caught and reported. You may be contacted with more questions about this bug at a later time.</h1>";
	parent::C();
}

function R(){
}
}
?>

Fatal error: Undefined class constant '__construct' in

This is due to auto loading it...

Here, i'm just going to post the whole freakin thing:

 

I've got an index page. EVERYTHING in the WHOLE site come through the index page.

The index page includes the MySql class, so, it's already available for any other scripts included in the page later.

 

I have a script that is supposed to insert a record into the database, but i purposely name one of the fields wrong, so that i can test my Bug Class. I will include all the code in a second.

 

If the query returns FALSE then the Bug Class is called. The Bug class's constructor accepts one argument -- the error details. It has NO OTHER methods. It takes the details, along with some server variables, and inserts them into my Bugs table.

 

The Bugs class Extends MySql -- which is already included via the index page.

 

The problem is, i am NOT able to catch the mysql_error and pass it along to the Bug class. BUT, it the MySql class, when calling a query, i can say "or die(mysql_error)", and it will give me the error just fine. This is crap... Here's the codes:

 

Insert statement:

	$r = $db->Q("insert into WretchedComments (uid,fromid,ctime,message,subject,label,mode)values('".$f->DbSafe($_POST['name'])."','{$_COOKIE['uid']}','".date("Y-m-d H:i:s")."','".$f->DbSafe($_POST['comment'])."','".$f->DbSafe($_POST['subject'])."','unread','$t1')");
if ($r != true){
	$b =  new Bug("Could not insert new comment: ". mysql_error());
}

And here's the mysql function for queries:

function Q($sql){
	$query = mysql_query($sql);// or die(mysql_error());
	return $query;
}

and here's the bug class:

<?php
class Bug extends MySql{
private $host;
private $uri;
private $uid;
private $desc;
private $ctime;

function __construct($description){
	$this->host= $_SERVER['HTTP_HOST'];
	$this->uri = $_SERVER['REQUEST_URI'];
	if (isset($_COOKIE['uid'])){
		$this->uid = stripslashes(str_replace("'","''",$_COOKIE['uid']));
	}else{
		$this->uid = "Guest, or Not Logged In";
	}
	$this->desc= $description;
	$this->ctime=date("Y-m-d H:i:s");
	parent::__construct();
	parent:();
	parent::Q("insert into BugReports (host,uri,uid,ctime,description)values('{$this->host}','{$this->uri}','{$this->uid}','{$this->ctime}','{$this->desc}')");
	parent::C();
	echo "<h1>There was a bug, but it was caught and reported. You may be contacted with more questions about this bug at a later time.</h1>";
}

function R(){
}
}
?>

Now, when there IS an ERROR, the bug class will output it's error message, but it won't catch the error from the previous mysql statement, and insert it into the db.

One of the ways i was tryig this, it would insert the bug report, but without the mysql_error. Now, i've messed with it so much, it's not even doing that.

if i add this to the end of my bug report's insert statement, like so:

$con->Q("insert into BugReports (host,uri,uid,ctime,description)values('{$this->host}','{$this->uri}','{$this->uid}','{$this->ctime}','{$this->desc}')") [color="Red"]or die (mysql_error())[/color];

It gives my the OTHER INSERT STATEMENT'S error Message!?!? Why would it do this? This must be why it's not getting entered into the DataBase.

if i add this to the end of my bug report's insert statement, like so:

$con->Q("insert into BugReports (host,uri,uid,ctime,description)values('{$this->host}','{$this->uri}','{$this->uid}','{$this->ctime}','{$this->desc}')") [color="red"]or die (mysql_error())[/color];

It gives me the OTHER INSERT STATEMENT'S error Message!?!? Why would it do this? This must be why it's not getting entered into the DataBase.

I tried to use mysql_free_result(); but it's not working either. With the following code, i get the following error:

	$r2 = $db->Q("insert into WretchedComments (uid,fromid,ctime,message,subject,label,mode)values('".$f->DbSafe($_POST['name'])."','{$_COOKIE['uid']}','".date("Y-m-d H:i:s")."','".$f->DbSafe($_POST['comment'])."','".$f->DbSafe($_POST['subject'])."','unread','$t1')");
if ($r2 != true){
	mysql_free_result($r2);

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource

Your code is seriously all over the place. Why does Bug extend MySql? It has nothing at all to do with database abstraction.

 

As for your last error, read what it says. supplied argument is not a valid MySQL result resource. You check it $r2 equals false, if it does you call mysql_free_result passing the bool false as its argument.

It doesn't matter anymore, i've gotten past that part, to the part that allows me to echo the error from the bug script, so i can access it now, but when i try to insert it into the db, it acts as if it were an object instead of a string. I fkn quit. I'm just going to have to use a seperate page, and pass the mysql error as a query, and then catch it. Till then, it's not being treated as a string.

4 hours wasted on a stupid mistake that i have never made before: forgetting to ESCAPE the single quotes!

 

Here's the finished product, which works exactly as it should've to begin with:

class Bug extends MySql{

function __construct($description){
	$h = $_SERVER['HTTP_HOST'];
	$q = $_SERVER['REQUEST_URI'];
	if (isset($_COOKIE['uid'])){
		$u = stripslashes(str_replace("'","''",$_COOKIE['uid']));
	}else{
		$u = "Guest, or Not Logged In";
	}
	$d = str_replace("'","''",$description);
	$t = date("Y-m-d H:i:s");
	parent::Q("insert into BugReports (host,uri,uid,ctime,description)values('{$h}','{$q}','{$u}','{$t}','{$d}')");
	echo "<h1>There was a bug, but it was caught and reported. You may be contacted with more questions about this bug at a later time.</h1>";
}
}

Inserting errors into a log stored in a database is a poor design anyway (IMO), what if the db goes down causing errors? You'll never know.

Well, if the DB Goes awry, that's GoDaddy''s problem, as i pay for hosting now. But thenx for the help.

 

Oh, and i kow that mysql isn't needed to be extended, but it's already included in a parent script, so i use it like that for simplicity :D

 

Thanx again, this one had me FUMING!

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.