I am seeing some very strange behavior today and I do not know what to make of it. We've been using codeigniter on our site for more than a year now and today when I clicked on one of our pages I saw a database error shown to me. I quickly opened up the controller for that page and refreshed to see it again but it did not happen this time, and I couldn't get it to trigger there again.
Hmm, alarming.
I navigated to a few other pages and it happened again on another, this time I looked at where it failed and it died on a query like this:
SELECT `foo`.`ID_NO` JOIN `bar` ON `bar`.`foo_ID` = `foo`.`ID_NO` WHERE `someSetting` = 0 AND `bar`.`Filter_ID` = '123'
...wait a second, where's the 'FROM foo' that should be in there?
I looked at my models controller and the following lines of code are what it tried to execute:
$this->db->select("foo.ID_NO");
$this->db->from('foo');
$this->db->join('bar','bar.foo_ID = foo.ID_NO');
This is code that has worked for a year and an error like this, anywhere, has not occurred before. It very much appears as though "$this->db->from('foo');" simply didn't happen.
I navigated to a bunch of other pages on the site and it happened a few times more, again same problem where the query wasn't being built correctly and a database error was thrown. When refreshed the error always went away. I haven't seen a repeat on any pages, nor have I seen a page pass then fail at a later time.
There are two things that maybe could be a factor: First, I recently added an error/exception handler (But that shouldn't have an impact on this, especially not once and then not again as far as I can tell).
Second, when I started using the error/handler I found an error in CodeIgniters that was known and was fixed by changing "function is_loaded($class = '')" to "function &is_loaded($class = '')" in Common.php because of an error that happened in Loader.php: "$this->_base_classes =& is_loaded();". That fix seemed to be widely accepted, and the error I'm experiencing is not repeatable and doesn't seems related so I also don't believe that is the cause.
I am using CodeIgniter version 2.1.2. Has anyone seen anything like this before? I can't imagine what the cause of this is but I would hate for it to happen outside the dev environment where I saw it happen today.
EDIT: Also, my fancy new error logging system I mentioned did not log anything that went wrong in the db->from function :(
The cause was my error catcher getting triggered due to referencing an index that didn't exist:
if($post_array['query'] != ""){
The error logger then gathered it's info and inserted into the database. I guess that it uses the same $this->db->ar_from field when you insert this way:
$this->db->insert('error_logging', $data);
Then clears it out afterwards as it does after any successful query.
The code then returned to it's previous position, with it's ar_select, ar_like, etc. fields intact but not ar_from and moved onto the $this->db->get() command which failed.
On subsequent page loads the error was already logged in the db and so a new insert was not performed and the query builder was not tampered with, and so no new database errors.
The fix was to simply get a clone of $this->db and use that clone to perform the insert.