Can't use database library in CI - codeigniter

For some reason I can't get the database library to work. Any time I put the line $this->load->database(); the script stops executing without returning any errors.
This is my database.php:
$db['default']['hostname'] = '127.0.0.1';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'employee';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;
This is my model:
class Customers_model extends CI_Model
{
function __construct()
{
parent::__construct();
//$this->load->database();
}
function add_customer($data)
{
echo "BEFORE CHECK";
$this->load->database();
echo "OK";
}
}
As I said, it doesn't return any errors. So in this case the screen will return "BEFORE CHECK" but it wont: "OK". Any ideas?

Try to change persistent connection $db['default']['pconnect'] = TRUE; to $db['default']['pconnect'] = FALSE; Or you can enable error log in config file from $config['log_threshold'] = 4; where Threshold options :
0 = Disables logging, Error logging TURNED OFF
1 = Error Messages (including PHP errors)
2 = Debug Messages
3 = Informational Messages
4 = All Messages

Make sure that you have 'error_reporting = E_ALL' in your php.ini to display any errors that occurs.
After that, if you have wrong credentials you should see the error type.
In CodeIgniter (CI) is very easy to work with Database.
See:
http://ellislab.com/codeigniter/user-guide/database/examples.html

Try changing
$db['default']['hostname'] = '127.0.0.1';
to
$db['default']['hostname'] = 'localhost';
make sure for the password as well

Ok, so it looks like everything started working fine as soon as I uncommented this line in php.ini: extension=php_mysql.dll
However why it was not returning any errors in CI ?

Are you sure you are in "development" mode? (look at the beginning of the index.php)
What happens if you manually trigger an error? trigger_error('blah blah', E_USER_ERROR);
edit: The problem may be because CodeIgniter masks many SQL-related errors (for instance see this recent issue). The easiest solution to debug your code would be to install Xdebug and enable the scream option.

Related

connecting to google cloud with codeigniter on appengine

i'm using codeigniter on google appengine ,
my issue is that appengine is unable to run query on the cloud sql ,
the my app on the appengine is able to connect to the cloud sql,
i have same setting in my local host and it's able to connect and run query on cloud sql,
i tried to debug the query function in the codeigniter , and i confirmed that both my appengine and my application in my local host are using the same exact query :
SELECT *
FROM (`grs_user`)
WHERE `username` = 'Admin'
AND `password` = '*******'
AND `is_published` = 'True'
i confirmed this by printing out the $sql var in get_where() function inside DB_active_rec.php .
database.php :
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = null;
$db['default']['database'] = 'fsm';
$db['default']['dbdriver'] = 'mysql';
$db['default']['pconnect'] = TRUE;
$db['default']['dbprefix'] = 'grs_';
$db['default']['swap_pre'] = '#__';
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['autoinit'] = TRUE;
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['cachedir'] ='';
$db['default']['stricton'] = FALSE;
$db['default']['socket'] = '/cloudsql/fsmocs:asia-east1:fsm';
database name is correct ,
anyone had such issue ?
after using the first generation SQL it's working now ,
i used same setting and same DB .sql file .

Primary user of Codeigniter

<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
I read the user guide carefully and try to run the code.first i create code in notepad which are above and then save it to the application/controller.but it give the error
A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: D:\xampp\htdocs\ci_intro\system\database\DB_driver.php
Line Number: 124
which is to be made change to run this code successfully.please anyone can help me.i am a primary user of codeigniter.so i don't understand the error.
Seems that you are trying to connect to database. If that is the case check your settings in config/database.php
If you don't want to connect to database, check your config/autoload.php and make sure that you are not autoloading the database library.
http://ellislab.com/codeigniter/user-guide/database/connecting.html
If you want to use database then you should fill out the config/database.php with the mysql db info. (I see you are using xampp so DB Driver would be mysql I suppose)
Lets say we have following database information: databaseName, databaseUser and databasePassword;
Soo the database.php would be filled out like this:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'databaseUser';
$db['default']['password'] = 'databasePassword';
$db['default']['database'] = 'databaseName';
$db['default']['dbdriver'] = 'mysql';
you wrote a right code. this error occur when you try to connect to your database with wrong config. to change your configuration go to application > config folder and open database.php file.
in that you see some config like this.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'databaseName';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;
before these configs they completely described and say how you can use them. read them and set your config.
this reference is useful to understand how you can config your database. if you config your database you can run your code without any problem.

CodeIgniter CLI returns blank when utilizing a model. How to run cron securely?

I am trying to run a cron from the command line utilizing CodeIgniter.
Initially it was returning a blank but utilizing the Codeigniter forum I found there was a bug in version 2.1.3 which required a line edit in Input.php
That was fixed. Still it wasn't working, it was just loading my homepage.
More searching led me to change my uri_protocol to AUTO in my config, and finally the CLI was working with the example outlined on the CI website.
However when I have utilized a model in the cron controller, once again the CLI returns blank bangs head
The controller
<?php
class Cron extends CI_Controller
{
public function admin_update()
{
$this->load->model('admin_model');
$this->admin_model->admin_cron();
}
}
The model function
function admin_cron()
{
$this->load->database();
echo "two";
}
It seems to be the $this->load->database() line that is breaking it.. as in if i remove this it outputs 'two'..
Does anyone have any idea why?
Thanks
You are not providing any input for $this->load->database();
Instead it should be something like this
var $DB; //global variable
$this->DB = $this->load->database('database1',TRUE);
use $this->DB for running the db operations now like,
$this->DB->query($query);
Put these config enteries in the config/database.php
$db['database1']['hostname'] = 'serverIP';
$db['database1']['username'] = 'username';
$db['database1']['password'] = 'pwrd';
$db['database1']['database'] = 'DB name';
$db['database1']['dbdriver'] = 'mysql';
$db['database1']['dbprefix'] = '';
$db['database1']['pconnect'] = FALSE;
$db['database1']['db_debug'] = TRUE;
$db['database1']['cache_on'] = FALSE;
$db['database1']['cachedir'] = '';
$db['database1']['char_set'] = 'utf8';
$db['database1']['dbcollat'] = 'utf8_general_ci';
$db['database1']['swap_pre'] = '';
$db['database1']['autoinit'] = TRUE;
$db['database1']['stricton'] = FALSE;
PS: http://ellislab.com/codeigniter/user-guide/general/models.html#conn
above link is on how to connect to a database

CodeIgniter Session - i am not getting my userdata in other controller

here is my code to set session
$login_data = array('logged_in'=>TRUE, 'user_id'=>$userid);
$this->session->set_userdata($login_data);
redirect('dashboard');
above code creates a new entry in ci_session table in my db and it has logged_in value
In dashboard controller i am checking session. Below is my code in dashboard controller
function __construct(){
parent::__construct();
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in){
redirect("welcome");
}
}
In above constructor block i am not getting the value of logged_in.
I have also tried to print all my userdata using var_dump($this->session->all_userdata) but all I get is an empty string.
Please tell me what could be the reason behing it. I also searched for cookie but i didn't find that in my computer. below is my config detail
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
I have solved my problem. I just forgot to change my Cookie Related Variables in config. –
My first guess is being you're doing this in the __construct method which is executed when the class is initialized - before the page is even loaded and your sessions data is set.
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in){
redirect("welcome");
}
Will work if you add in into the beginning of the page method.
Is the library being autoloaded? Check config/autoload.php:
$autoload['libraries'] = array('database', 'session', 'form_validation');
Or call $this->load->library('session'); right before using the class.

cannot connect to database using codeigniter

I know this sounds so repost several times. But I cannot get it resolved.
I tried to connect my application to mysql, these are what I have tried (I tried only one out of these options, not both):
$autoload['libraries'] = array('database'); on application\config\autoload.php, but I got Fatal error: Call to a member function get() on a non-object in /Users/zakkafm/Sites/CodeIgniter/application/models/My_models.php on line 15
$this->load->database(); on application\models\My_models.php on __construct() (also I added parent::__construct(), but I got Fatal error: Call to a member function database() on a non-object in /Users/zakkafm/Sites/CodeIgniter/application/models/My_model.php on line 7
So, what could be wrong here?
EDIT:
this is my database.php looks like.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'trial';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;
RESOLVED!!!
After all, this is all because of the autocomplete on Eclipse I added on system/core/Controller.php and system/core/Model.php.
Hmm.., I think you don't have to add
$this->load->database();
on your My_models.php, coz as I know it will be automatically connected to the database as you set on the database.php file. So, remove it from My_models.php file & try again.
Hope it helps :)
In you My_models.php you shouldn't be calling $this->load->database(); should just have:
class My_models extends CI_Model {
/**
* Constructor
*/
function __construct()
{
parent::__construct();
}
function something(){
// your model code goes here
}
}
Also you only need to enable the autoload and you are good to go (keeping in mind that the MYSQL library needs to be loaded for PHP.
And you might have an error here (don't see your code so not sure):
Fatal error: Call to a member function get() on a non-object in
/Users/zakkafm/Sites/CodeIgniter/application/models/My_models.php on line 15

Resources