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
Custom errors - Your advice
Moderator: General Moderators
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.");
?>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();
}
}
}