Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] CI Database Class not returning query results, but MySQLI does.
#1

[eluser]Unknown[/eluser]
--SOLVED!--
Problem was that third party host did not support the mysql extension, only the mysqli extension. As soon as I changed the database.php config file row to say "mysqli" instead, everything worked. Hopefully this can help someone.

Using CodeIgniter 2.1.3

I recently moved my website project from my localhost (apache server) to a third party host online.
But now, CodeIgniter can't seem to get any rows from the databse. The database.php config file is the same, everything else is the same! The only thing that changed was any changes that might come from moving my website project folder from my computer to another place (file paths, etc?). The EXACT SAME CODE works on my localhost, but it doesn't work when uploaded to the third party host.

From the below code I'm creating a string I will send to an Exception so that I can watch the results in the browser. The string is of two parts. The first part shows the results from the CI Database Class, then separated by a colon (Smile, then the second part is the result from the MySQLi query.
The CI Database part is always empty, but the MySQLi query part is filled with correct results.
This shows that the there is something wrong with the database class or the connection to the database, since the MySQLi example with the exact same query works.

What have I missed? This is REALLY frustrating. For three days I couldn't come up with a reason to why it might not work. I hope you can help!

Code:
//Test with CI Database Class

$this->load->database();
$query = $this->db->query('SELECT * FROM challenge');
$test = "CI Db Query: ";
foreach ($query->result() as $row)   //Always empty!!
{
$test .= $row->title;
}
//Test with MySQLI
$test2= "MYSQLI Query: ";
$con=mysqli_connect("my_host","my_user","my_password","my_db");
$result = $con->query("SELECT * from challenge");

while ($row = $result->fetch_assoc()){
$test2.= $row["title"];
}
throw new Exception($test. " : ". $test2); //For the exception to show in the browser as an Internal Error

This is my database.php config file
Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'my_host';
$db['default']['username'] = 'my_user';
$db['default']['password'] = 'my_password';
$db['default']['database'] = 'my_db';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

The result is:

> Uncaught exception 'Exception' with message 'CI Db Query: : MYSQLI
> Query: Title1Title2Title3
#2

[eluser]jonez[/eluser]
If you're using mysqli this line should read;
Code:
$db['default']['dbdriver'] = 'mysqli';

Try changing the CI query to "SELECT 'test123' AS title" and see if it returns a row. If it doesn't your database connection isn't working (check credentials).

[edit]
See you found it Smile
[/edit]
#3

[eluser]Unknown[/eluser]
Yea, on my computer I had both the mysql and mysqli extension installed, so it didn't matter which one I was using.
The third party host only used mysqli, which is why I couldnt query the database Smile




Theme © iAndrew 2016 - Forum software by © MyBB