Page 1 of 2
problem with throwing error
Posted: Thu Jun 21, 2007 11:28 am
by staar2
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have mysql handler class but, it doesn't handle connection error
Code: Select all
public function __construct($host, $user, $pass, $database) {
$this->connection = mysql_connect($host, $user, $pass); -- this is problem, i don't know how to handle this?
if (!is_resource($this->connection)) {
throw new RMysqlException(mysql_error());
}
if (!mysql_select_db($database, $this->connection)) {
throw new RMysqlException(mysql_error());
}
}
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Jun 21, 2007 11:56 am
by volka
If it doesn't handle an error while connecting the databaserver what does
if (!is_resource($this->connection)) {
throw new RMysqlException(mysql_error());
}
do?
Posted: Thu Jun 21, 2007 12:11 pm
by staar2
hmm yes but if i make some mistake by typing host or username wrongly then it wont catch error

Posted: Thu Jun 21, 2007 12:21 pm
by volka
Works fine for me
Code: Select all
<?php
class RMysqlException extends Exception {
// is this really an exception class used for only one other class?
}
class RMysql {
public function __construct($host, $user, $pass, $database) {
$this->connection = mysql_connect($host, $user, $pass);
if (!is_resource($this->connection)) {
throw new RMysqlException(mysql_error());
}
if (!mysql_select_db($database, $this->connection)) {
throw new RMysqlException(mysql_error());
}
}
}
try {
$o = new RMysql('wrong', 'credentials', 'foo', 'bar');
}
catch(Exception $e) {
echo 'exception message: ', $e->getMessage();
}
If you want to suppress the warning message try
@mysql_connect(...
Posted: Thu Jun 21, 2007 12:26 pm
by staar2
But is there any other way i don't really like to hide errors ?
External error:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'localhosst' (11001) in E:\wamp\www\test\classes\database.class.php on line 8
Posted: Thu Jun 21, 2007 1:15 pm
by RobertGonzalez
Can you use mysqli instead of mysql? It has a built in function called
mysqli_connect_error().
Posted: Thu Jun 21, 2007 1:28 pm
by volka
staar2 wrote:But is there any other way i don't really like to hide errors ?
You don't hide an error. You suppress a superfluous warning output because you handle the error yourself.
Posted: Thu Jun 21, 2007 2:01 pm
by staar2
Ty all, i'll add @.
Posted: Thu Jun 21, 2007 2:15 pm
by david2007
staar2 wrote:But is there any other way i don't really like to hide errors ?
External error:
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'localhosst' (11001) in E:\wamp\www\test\classes\database.class.php on line 8
You can handle you error and then use the header function with a 500 http error to the browser.
if you're using ajax, it works perfectly.
MVC is complex, Web2.0 is simpler.
Sybrain Framework.
http://www.sybrain.com
Posted: Thu Jun 21, 2007 2:39 pm
by superdezign
staar2 wrote:But is there any other way i don't really like to hide errors ?
staar2 wrote:Ty all, i'll add @.
I think if anything is considered "hiding an error," it's @.
david2007 wrote:You can handle you error and then use the header function with a 500 http error to the browser.
if you're using ajax, it works perfectly.
MVC is complex, Web2.0 is simpler.
.... What?
Why would you want to give the user a server error for anything?
Why would you make a PHP script depend on AJAX?
What do you mean by the last part, and how does it tie in to this?
You've confused me. Explain.

Posted: Thu Jun 21, 2007 2:52 pm
by RobertGonzalez
I still think you should use mysqli.
Posted: Thu Jun 21, 2007 3:28 pm
by volka
superdezign wrote:I think if anything is considered "hiding an error," it's @.
not necessarily.
What does the echo'd warning provide that mysql_error() and the exception doesn't provide?
What is hidden? It's not (immediately) printed to the browser ...but exactly that is the unwanted "feature".
Posted: Thu Jun 21, 2007 3:40 pm
by staar2
oh k i found that mysqli is installed to my server so i can use that. It should be better then mysql right ?
Posted: Thu Jun 21, 2007 4:40 pm
by RobertGonzalez
It has a lot more features in it, including OOP support built right in. It is not exactly the same as mysql though, so you should read up a little bit to get used to it.
Posted: Fri Jun 22, 2007 3:12 am
by staar2
K now i rewrote the class.. but it looks like with mysqli there is no point to make database handler class.
Code: Select all
class RMysql {
private $db;
private $result;
public function __construct($host, $user, $pass, $data) {
@$this->db = new mysqli($host, $user, $pass, $data);
if (mysqli_connect_error()) {
throw new RMysqlException('Connecting problem !');
}
}
public function query($query) {
$this->result = $this->db->query($query);
if (!$this->result) {
throw new RMysqlException('There were problem with returning result!');
}
}
public function getResult() {
return $this->result;
}
public function __destruct() {
$this->result->close();
$this->db->close();
}
}
class RMysqlException extends Exception {
public function __construct($message) {
parent::__construct($message);
}
/*
* Call out when exception rises, maybe display to user error or
* just move to first page!
*
* Poblem if there is error on first page it is then endlesss loop
*
*/
public function log() {
echo parent::getMessage();
echo parent::getLine();
echo parent::getFile();
echo parent::getTraceAsString();
exit();
}
/*
* Writes to file error, if file is in some size it makes another
* file and writes there, if that is full it makes new file and so on...
*/
protected function write($msg, $filename) {
$size = filesize($filename);
if (file_exists($filename)) {
$fp = fopen($filename, 'a');
if (is_resource($fp) && flock($fp, LOCK_EX)) {
fwrite($fp, $msg, $size);
flock($fp, LOCK_UN);
}
fclose($fp);
return true;
}
}
}
try {
$db = new RMysql('localdhost', 'root', '', 'test');
$db->query('SELECT * FROM feedback');
while ($row = mysqli_fetch_assoc($db->getResult())) {
echo $row['name'] .'<br />';
}
}
catch (RMysqlException $r) {
$r->log();
}
catch (Exception $e) {
echo 'There where some unknown error!';
}