Using mySQL error information

Discussion in 'MySQL' started by MikeHayes, Jun 15, 2010.

  1. #1
    I'm getting an error with a CREATE table query. I know what the problem is (I'm creating tables dynamically and now and then a name slips through with a space or other illegal character.)
    The error occurs in a function and what I want to know is, is there a system field/flag which I can use after the return in an if statement to detect whether an error has taken place or not.
    Thanks,
    Mike
     
    MikeHayes, Jun 15, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use a try catch coding style to get and/or log your errors. Assuming php - http://php.net/manual/en/language.exceptions.php

    You can also use simple logic to throw or log the error, but not kill the script.

    if(!$query) {
    log(mysql_error());
    }
    PHP:
    Or you could make a simple script to collect the errors and display them at the end.

    
    if(!$query) {
        $errors[] = mysql_error();
    }
    
    //At the end of the script
    if($errors) {
        foreach($errors as $key => $error) {
            echo $error;
        }
    }
    
    PHP:
     
    jestep, Jun 16, 2010 IP
  3. MikeHayes

    MikeHayes Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. I´ll give it a try.
    Mike
     
    MikeHayes, Jun 16, 2010 IP