Hi,

Sunday, January 5, 2003, 12:47:40 PM, you wrote:
AP> How about something like this..
 
AP> function runquery($query)
AP> {
AP>      $query = mysql_query($query);
AP>      if (mysql_error())
AP>      {
AP>          echo "<div style=\"color:red\">MySQL Error: ". mysql_error() ."</div>\n";
AP>          exit(1);
AP>      }
     
AP>     return $query;
AP> }
     

AP>         -----Original Message----- 
AP>         From: Jeff Lewis [mailto:[EMAIL PROTECTED]] 
AP>         Sent: Sat 1/4/2003 6:43 PM 
AP>         To: [EMAIL PROTECTED] 
AP>         Cc: 
AP>         Subject: [PHP] Function to catch all mySQL errors?
        
        

AP>         I know that mySQL errors are caught in mysql_error() and I find that
AP>         function extremely useful (kudos!). However, I have several queries in a 
few
AP>         scripts of mine but am wondering if anyone has written a small function 
that
AP>         catches errors and outputs them. What I mean is lets say I have a block of
AP>         code like so:
        
AP>         $result = mysql_query("SELECT field1, field2 FROM users WHERE userLogin =
AP>         '$authusername' AND userPassword = '$authpassword'");
        
AP>         Instead of a small loop under my query that is like so:
        
AP>         if (mysql_error()) {
AP>          echo "Error: ".mysql_error();
AP>          exit;
AP>         }
        
AP>         I'd like to be able to call a function that does this instead of adding 
this
AP>         if statement after all my queries...so...anyone do something similar?
        
AP>         Jeff
Here is a small class I use for mysql that will do what you want.

<?
class mysql_class{
        var $connection =
        
array('db'=>'database','host'=>':/tmp/mysql.sock','user'=>'username','password'=>'password');
        var $handles = array();
        var $con;
        function mysql_class($db='',$ini=''){
                global $class_ref;
                $class_ref["mysql_class"] =& $this;
                if($ini != '' && file_exists($ini)){
                        $this->connection = parse_ini_file($ini,TRUE);
                }
                if(!$this->con = @mysql_connect($this->connection['host'] , 
$this->connection['user'] , $this->connection['password'])):
                        echo "oops failed to connect <br>";
                else:
                        if($db != '')   mysql_select_db($db,$this->con);
                        else mysql_select_db($this->connection['db']);
                endif;
        }
        function get_handle($db=''){
                $r = 0;
                if(!empty($this->con)):
                        $r = count($this->handles) + 1;
                        if($db == '') $db = $this->connection['db'];
                        $this->handles[$r]['db'] = $db;
                endif;
                return $r;
        }
        function select_db($h){
                mysql_select_db($this->handles[$h]['db'],$this->con);
        }
        function sql_query($sql,$h,$line=''){
                $r = 0;
                if(!empty($line)){
                        $line = "on line $line ";
                }
                if($sql != ''):
                        $this->select_db($h);
                        if(!$r = mysql_query($sql,$this->con)):
                                echo 'Oops error '.$line.': 
'.mysql_error($this->con).'<br>';
                        endif;
                endif;
                return $r;
        }
        function num_rows($result){
                return mysql_num_rows($result);
        }
        function sql_insert($sql,$h,$line=''){
                $r = 0;
                if($sql != ''):
                        $this->select_db($h);
                        if(!$r = mysql_query($sql,$this->con)):
                                echo 'Oops error at '.$line.': 
'.mysql_error($this->con).'<br>';
                        else:
                                $r = mysql_insert_id($this->con);
                        endif;
                endif;
                return $r;
        }
        function fetch_array($res,&$row,$type=MYSQL_ASSOC){
                $r = 0;
                if($row = mysql_fetch_array($res,$type)) $r = 1;
                return $r;
        }
        function fetch_result($res,$field){
                return mysql_result($res,0,$field);
        }
}

//usage
        
 $mysql = new mysql_class();
 $con = $mysql->get_handle('test_database');
 $result = $mysql->sql_query("SELECT * FROM test_table",$con,__LINE__)):
 .
 .
 .
 $insert_id = $mysql->insert("INSERT INTO test_table (id,name) VALUES 
($id,'$name')",$con,__LINE__);
 .
 .


 If it errors it will print an error and give the line number of the call.
 It can handle multiple databases in the same class just get another handle.
 Hope this helps
 Tom
 

 



-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to