In database.php I have:
$db['default']['username'] = IS_PRODUCTION ? 'prodUsername' : 'localDevBoxUsername';
$db['default']['password'] = IS_PRODUCTION ? 'prodPassword' : 'localDevBoxPassword';
My code is:
function test()
{
$this->load->database();
$result = $this->db->query('SELECT * FROM items WHERE description='Oranges');
echo 'rows:' . $result->num_rows();
}
When I run this in prod it fails.
Now to test it’s not a database config issue, I created a basic PHP file called test.php with:
<?
if (! $db=@mysql_connect ("localhost", "prodUsername", "prodPassword"))
die("Error connecting to mysql");
@mysql_select_db("prodDatabase") or die("error connecting to specific database");
$result = @mysql_query('SELECT * FROM items WHERE description='Oranges');
echo 'rows: ' . mysql_num_rows($result);
?>
The php test script works perfectly. The codeigniter code only works for my local dev box but not the prod box. What could I possible be missing????
Update: Please note that although I’m using a constant IS_PRODUCTION in the sample code, this isn’t the problem. I’ve pulled out the values and hardwired them. I just included it to make the example more succinct, that it so show that it works in one environment and not the other
Update2: The issue seems only to be related to the Model. As soon as I move the code to the Controller it works…
The issue turned out to be the naming conventions used for the filenames of the models. In my local development environment the case sensitivity required by CodeIgniter wasn’t enforced for whatever reason (some configuration in PHP, etc., I don’t know – I was using xmapp for local desktop development on this specific project). Once the model files were named according to the CodeIgniter specs everything worked as planned.
Therefore if you’re database code works in the Controller but not in the model, definitely confirm your model file named conventions (as well as class constructors).