#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Maine
    Posts
    18
    Rep Power
    0

    Failing to pull the mysql_error() value using a variable


    Hello All,

    I'm creating a small error logger in my php code but I'm having issues getting the 1 variable to work with mysql_error().

    Can anyone see what I'm doing wrong? The only issue I have for submitting to my 'logs' table is that 1 variable ($error) with the $sql2 variable. If I place 'text' there, it works fine.

    Code:
    if (!$con) {
        $error = mysql_error();
        $con2 = mysql_connect($dbhost,$dbun,$dbpw2);
        mysql_select_db($db1,$con2);
        $sql2 = "INSERT INTO logs (error,cause) VALUES ($error,'Database Connection Failure')"; // all other $error variables work as intended, just not this one
        mysql_query($sql2,$con2);
        die('Could not connect: ' . $error);
    }
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    $error is a string. That means it needs both escaping and quotes.
    PHP Code:
    "...VALUES ('" mysql_real_escape_string($error) . "', 'Database..." 

    Comments on this post

    • SiLeNCeD agrees : Perfect
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Maine
    Posts
    18
    Rep Power
    0
    Originally Posted by requinix
    $error is a string. That means it needs both escaping and quotes.
    PHP Code:
    "...VALUES ('" mysql_real_escape_string($error) . "', 'Database..." 
    I'm such a newb... Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo