echoing MySQL errors?

Discussion in 'PHP' started by moronic_kaos, Jul 29, 2010.

  1. #1
    What does this do exactly:

    echo mysql_error();
    PHP:
    I've noticed a lot of example scripts talking about echoing that function. But why, since wouldn't apache or whatever server echo it anyway as like a warning?
     
    moronic_kaos, Jul 29, 2010 IP
  2. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #2
    mysql_error() contains the error message (if there is one) from the most recent mysql query. a lot of times it is beneficial to include this in your error handling of mysql queries so you know what went wrong:

    Example:
    mysql_query("SELECT * FROM table") or die(mysql_error());
    Code (markup):
    Say for example that the table "table" does not exist in your database. This script will output and error telling you so and you can easily see what went wrong. Without manually outputing this error, the script simply will not retrieve the data and won't even let you know that there was an error.
     
    Thorlax402, Jul 29, 2010 IP
  3. ARTidas

    ARTidas Active Member

    Messages:
    177
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #3
    In case you do not get the error report, then you could print the last mysql error message.

    Cheers,
     
    ARTidas, Jul 29, 2010 IP
  4. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thats exactly it. not all errors are printed to the screen, and echoing this function fixes it.
     
    ThomasTwen, Jul 29, 2010 IP