Custom errors - Your advice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Custom errors - Your advice

Post by snicolas »

Hi All,

I was wondering if somebody had a nice solution to handle PHP errors.
We all have this kind of problems on some page. like:

"Warning: Mysql insert() error" etc...

What is the best way to handle this and not display errors, or display them but in a more user friendly appeareance?

Something like:
"Sorry, we have a problem, the table blah blah has encountered...."

You know what i mean


Stef
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

if its only while connecting or handling a mysql database use this:

Code: Select all

<?php
$sql = mysql_query("...") or DIE("There was an internal server error.");
?>
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

i already do this when connecting.
I was looking for something a bit more advanced to detect any SELCT, UPDATE, INSERT errors.

s
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

The best thing you can do is create a function or a class to handle your queries.
In case of an error you can use check which kind of query was done and display your error statement.
I usually do error nr and error detail given by mysql.
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

Thks ckuipers,

Any idea where i can find an example of this type of function?

s
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

This is a class I've written, you can update and expand on it if you like...

Code: Select all

class mysql_q
	&#123;
		var $query,
			$db_host,
			$db_user,
			$db_name,
			$db_pass,
			$db_link,
			$result,
			$num_affected,
			$insert_id,
			$num_selected,
			$check,
			$error;

		function mysql_q()
		&#123;
			$this->db_host= _DB_host;
			$this->db_user= _DB_username;
			$this->db_name= _DB_database;
			$this->db_pass= _DB_password;
		&#125;

		function connect()
		&#123;
			$this->db_link= mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);

			if (!$this->db_link)
			&#123;
				$this->check= 0;
				$this->error= mysqli_connect_error();
			&#125;
			else
				$this->check= 1;
		&#125;

		function disconnect()
		&#123;
			mysqli_close($this->db_link);
		&#125;

		function query($query)
		&#123;
			$this->connect();
			if ($this->check == 1)
			&#123;
				$this->result= mysqli_query($this->db_link, $query);
				if (!$this->result)
				&#123;
					$this->check= 0;
					$this->error= mysqli_errno($this->db_link) . ": " . mysqli_error($this->db_link);
				&#125;
				else
				&#123;
					$check= trim(strtoupper($query));
					switch ($check&#123;0&#125;)
					&#123;
						case 'U':
						case 'D':
							$this->num_affected= mysqli_affected_rows($this->db_link);
							break;
						case 'S':
							$this->num_selected= mysqli_num_rows($this->result);
							break;
						case 'I':
							$this->insert_id= mysqli_insert_id($this->db_link);
							break;
					&#125; 
					$this->check= 1;
				&#125;
				$this->disconnect();
			&#125;
		&#125;
	&#125;
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

Post by snicolas »

thanks again ckuipers
It's a great start..will look into it.
Post Reply