Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Sql insert statement sometimes working other times not working

0

52 views

Im building an application and Im having a bizarre issue where sometimes an sql statement will work and the very next call the exact same sql statement simply will not work.

I personally suspect its the hosting provider Im using maybe just not being fast enough to accept large volumes of sql calls to the database, but I figure I will atleast ask to see if anyone else has ever had similar issues

asked June 25, 2011 11:05 am CDT
posted via StackOverflow

2 Answers

0
 

Assuming you don't have access to the mysql general query log if you're on a shared host and can't modify the database's my.cnf file, you might draft up a function along the lines of:

function dbInsertQuery($query, $conn) {
     $logFile = "/path/to/file.txt";
     $isProd  = FALSE;

     // run your security checks on the query here

     $mysql_query($query, $db_connection) or trigger_error(mysql_error());

     if (!$isProd && is_writable($logFile)) {
       if (!handle = fopen($logFile, 'a')) {
         echo "cannot open dbInsertQuery's log file: " . $logFile;
         exit;
       } 

       $logLine = date(DATE_RFC822) . " - " . $query;

       if (fwrite($handle, $logLine) === FALSE) {
         echo "cannot write to dbInsertQuery's log file: " . $logFile;
         exit;
       }

       fclose($handle);
     }

   }

This will allow you to log all your queries to a log file. I set in a $isProd variable for you to toggle for development, and if it is set to the a value of TRUE, it won't log to the file, and will skip the who fopen process entirely. You may need to adjust it, and could even get it to run every query and not just inserts if you wanted.

answered June 25, 2011 1:05 pm CDT
0
 

It could be the values you are dynamically adding to the query statement. You should escape the values to make sure they do not break your query.

In PHP you can use the mysql_escape_string() function. Documentation here: http://us3.php.net/mysql_escape_string

Also if you are running the query from a browser, you can normally see the error if you do something like this:

$query = mysql_query("QUERY", $db_connection) or trigger_error(mysql_error());

answered June 25, 2011 1:05 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions