Re[4]: [PHP] mysql connect function in my class

From: Date: Thu, 15 Apr 2004 05:26:19 +0000
Subject: Re[4]: [PHP] mysql connect function in my class
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-183418@lists.php.net to get a copy of this message
Hi, Thursday, April 15, 2004, 2:57:48 PM, you wrote: >>How many versions of the wheel exist in the world >today? ...not many >>hacked out of stone I bet :-) >> >>-- >>regards, >>Tom AB> what does that mean?? is everybody saying i should give up on this idea AB> then?? What I am saying is that it doesn't matter what already exists if you want to do it your way go ahead and give it a go. On the original subject, if this is part of a class then I would set a class variable called $last_error. Set this to '' at the begining of your function and if there is an error then fill it with mysql info with in this case "Connection failed" or with other functions mysql_error(); That way if your function returns false you can do a call to your class->get_last_error(); The other thing I find useful is to pass the line info to the function so the error can have more meaning. A bit like this (untested): function Connect($host, $mysqluser, $mysqlpwd, $line=false){ $this->last error = ''; $link=mysql_connect($host, $mysqluser, $mysqlpwd) if(!$link){ $this->last error = "Connection failed using $mysqluser on $host"; if($line) $this->last error .= " on line:$line"; return false; } else { return $link; } } function get_last_error(){ return $this->last_error; } Then call it with if($hndl = $class->Connect($host,$mysqluser,$mysqlpwd,__LINE__)){ echo 'Connected <br>'; }else{ echo '<font color="red">'.$class->get_last_error().'</font><br>' } -- regards, Tom

« previous php.general (#183418) next »