problem with throwing error

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

staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

problem with throwing error

Post by staar2 »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

hmm yes but if i make some mistake by typing host or username wrongly then it wont catch error :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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(...
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you use mysqli instead of mysql? It has a built in function called mysqli_connect_error().
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

Ty all, i'll add @.
david2007
Forum Newbie
Posts: 9
Joined: Sat Nov 11, 2006 9:36 am

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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. :P
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I still think you should use mysqli.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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".
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post 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 ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post 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!';
}
Post Reply