Page 1 of 1
Custom errors - Your advice
Posted: Tue Jan 18, 2005 5:34 am
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
Posted: Tue Jan 18, 2005 6:51 am
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.");
?>
Posted: Tue Jan 18, 2005 7:04 am
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
Posted: Tue Jan 18, 2005 7:08 am
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.
Posted: Tue Jan 18, 2005 7:20 am
by snicolas
Thks ckuipers,
Any idea where i can find an example of this type of function?
s
Posted: Tue Jan 18, 2005 7:24 am
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
{
var $query,
$db_host,
$db_user,
$db_name,
$db_pass,
$db_link,
$result,
$num_affected,
$insert_id,
$num_selected,
$check,
$error;
function mysql_q()
{
$this->db_host= _DB_host;
$this->db_user= _DB_username;
$this->db_name= _DB_database;
$this->db_pass= _DB_password;
}
function connect()
{
$this->db_link= mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if (!$this->db_link)
{
$this->check= 0;
$this->error= mysqli_connect_error();
}
else
$this->check= 1;
}
function disconnect()
{
mysqli_close($this->db_link);
}
function query($query)
{
$this->connect();
if ($this->check == 1)
{
$this->result= mysqli_query($this->db_link, $query);
if (!$this->result)
{
$this->check= 0;
$this->error= mysqli_errno($this->db_link) . ": " . mysqli_error($this->db_link);
}
else
{
$check= trim(strtoupper($query));
switch ($check{0})
{
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;
}
$this->check= 1;
}
$this->disconnect();
}
}
}
Posted: Tue Jan 18, 2005 7:34 am
by snicolas
thanks again ckuipers
It's a great start..will look into it.