I'm trying to insert a number into the database from within a library using,
$data = array(
'id' => $id,
'so' => '1',
'username' => $username
);
$this->db->insert('db', $data);
but whenever I run it, I get "Fatal error: Call to a member function insert() on a non-object in"
Anyone know why?
Thanks
If you're doing this inside a library, you can't reference the $this object like you normally do in a model. Instead, you'll need to load an instance of CI like so:
$CI =& get_instance();
Then you should be able to insert a record into the database like this:
$CI->db->insert('db', $data);
Don't forget to load the database library as well if it's not in autoload.
In a library "$this" refers to the class that you are coding (OOP concepts).
The DB object is part of the CI superobject. So in order to use db in your class you will have get a reference to the CI object like this
$CI =& get_instance();
And then rewrite your insert statments like this
$CI->db->insert('db', $data);
Related
I have created a class MY_Model from which all my models inherit from. Inside it I have a method called switchConnection. Inside it I need to use system library encryption.
I know that in a model $this refers to the model so I can not do:
$this->load->library('encryption')
I have tried loading the library using:
$ci = & get_instance();
$ci->load->library('encryption');
$ci->encryption->decrypt(....);
but I get the same error: Trying to get property of non-object.
Any ideas?
I found a solution:
$this->load->library('encryption');
$myEncryption = new CI_Encryption();
$myEncryption->decrypt($string);
Class name is CI_Encryption
You should remove the space between '=' and '&'.
So try this way ->
$ci =& get_instance();
i have a problem im trying to deal with for a long time,
i have a codeigniter webapp that im building and i have a problem with the $this of codeigniter
i will show you with so examples:
this is in my controller
$this->site_model->blog_post('title');
this one work perfectly /\
if im loading an other / different model it will load it all good, but if im calling a function in the new model like this:
$this->Admin_model->blog_post('title');
it will give me this
Undefined property: Idx::$Admin_model
Call to a member function get_categories() on a non-object in
C:\dev\wamp\www\francebeautycoil\application\controllers\idx.php on line 22
if im doing the next code
public $ci = '';
$this->ci =& get_instance();
$this->load->model('Admin_model');
$this->ci->Admin_model->blog_post('title');
it will work.
ive tryed all the catital lettes, lowercase letters.
the thing is that its append not only on this model but on few libraries too.
please help, im stuck.
Every time you use an model you need to load it first , on second snipped you need to load Admin_model first
if im loading an other / different model
On second snppiet you are calling a method on Admin_model not loading it on line below
$this->Admin_model->blog_post('title');
so load it first
$this->load->model('Admin_model');
so it becomes
$this->load->model('Admin_model');
$this->Admin_model->blog_post('title');
I am building a SugarCRm Module. It uses the old basic MySQL functions from like PHP4 days...
Most modules in the system access a Database wrapper class calling this Global variable and then using it.
global $db;
$db->quote($some_SQL_Query_To_Execute);
Now i'm not much of a fan of that so in my own custom Module classes in my Constructor method I am instead doing this...
class MyModuleClassHereForSODemo
{
public $db;
public function __cnstructor($id){
$this->db = DBManagerFactory::getInstance();
}
public function insertDBRecordFunctionHERE($id){
$sql = "INSERT INTO `db_table_name` (
`topic_id`,
`project_id`,
`topic_subject`,
`topic_content`,
`date_created`,
`date_last_post`,
`created_by_user_id`,
`last_post_user_id`,
`posts_replies`
) VALUES(
NULL,
'$this->db->quote($project_id)',
'$this->db->quote($topic_subject)',
'$this->db->quote($topic_content)',
UTC_TIMESTAMP(),
UTC_TIMESTAMP(),
'$this->db->quote($created_by_user_id)',
'$this->db->quote($created_by_user_id)',
'0');";
$this->db->query($sql);
}
}
Now here is the problem I am having, this SQL query above will not work because I am calling $this->db->quote() on my variables...now what is extremely weird and frustrating is if I instead use the GLOBAL $db variable, then I can run this instead $db->quote() and it works all day long...it;s as soon as I run the quote() method on my own classes $db property that it breaks everything.
Could this possibly be because the system is looking at them as being different connections to the DB even though it;s a Singleton and should be the same variable and object I would imagine....any ideas?
Also I don'tr have all the code for the DB class that I am accessing but I do know that the quote method is simply calling mysql_real_escape_string
Quite late to the party here but the issue isn't that you have a local $db variable. It's that string parsing wont pick up the use of functions in your string. You should use the complex syntax instead.
I.e.
$string = "This is a string with a function call'{$this->db->quote($project_id)}'";
See https://php.net/manual/en/language.types.string.php#language.types.string.parsing
I am new to codeigniter, and I have developed a code to carry out queries on the database. I load the database using$this->load->database(); and perform a query, but when I run the code, the browser gives me the following error message:
A PHP Error was encountered Severity: Notice Message: Undefined property: Tutorial::$load.
Fatal error: Call to a member function database() on a non-object
This is the code I am using:
class Tutorial extends CI_Controller {
public function tutorial() {
$this->load->database();
$query = $this->db->query('SELECT user,pass,email FROM tablex');
foreach ($query->result() as $row) {
echo $row->title;
echo $row->name;
}
I am sure the $db variables in my database configuration file are properly set and I have even tried autoloading the database for all pages in the autoload.php config file; still having the same problem. Any ideas how to go about this?
Change
$this->load->database();
to
$this->load->library('database');
database is not a direct method. It is a library in codeigniter and you have to load it as a library.
You can also autoload database library in autoload.php.
UPDATE:
You are using the same name for your class and method. In PHP4, a method which has the same name as class name was treated as constructor, but if you are using codeigniter 2+, you have to use PHP5 constructor which is
function __construct()
{
parent::__construct();
/*Additional code which you want to run automatically in every function call */
}
You cannot give a method same name as class name in Codeigniter 2+. Change the method to anything else. You can name the method index if you want it to load by default.
This should solve your problem.
CodeIgniter User Guide, Creating Libraries Section:
To access CodeIgniter's native resources within your library use the
get_instance() function. This function returns the CodeIgniter super
object.
Normally from within your controller functions you will call any of
the available CodeIgniter functions using the $this construct.
$this, however, only works directly within your controllers, your
models, or your views. If you would like to use CodeIgniter's classes
from within your own custom classes you can do so as follows:
First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
Once you've assigned the object to a variable, you'll use that
variable instead of $this:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
Hope this helps. You could also put the $CI in a constructor.
Your code would look something like this:
class Tutorial
{
public $CI;
/**
* Constructor.
*/
public function __construct()
{
if (!isset($this->CI))
{
$this->CI =& get_instance();
}
$this->CI->load->database();
}
}
I created a new model class as my_model.php inside /models folder, and a function inside it to load all elements:
function get_all(){
$query = $this->db->get('test'); //test is my table
return $query->result();
}
In the controller, I instantiated the class and called the method;
$this->load->model('my_model');
$res = $this->my_model->get_all();
But, this throws me error saying:
Fatal error: Call to a member function get() on a non-object in /var/www/testapp/application/models/my_model.php on line 7
This line 7 points to the portion of the code where I have used $this->db. I tried to see the value of $db but I think it is magic accessor __get and __set, so I could not see the value of this property before calling that method.
I tried googling for several other cases but none of them match my scenarios and rather none of them could solve my problem.
You have to load the Database first
$this->load->database();
So, all code:
function get_all(){
$this->load->database();
$query = $this->db->get('test'); //test is my table
return $query->result();
}
Or, load database in your __construct method.
Or, IMO, It's better to autoload database by changing application/config/autoload.php, example is below.
$autoload['libraries'] = array('database','form_validation'); //form_validation is for example only
In CodeIgniter, you should load database model before you use it.
Use $this->load->database(); to load database model.
Your error is actually quite simple:
return $query->result;
Should be:
return $query->result();
Sometimes the line number reported by a PHP error isn't exactly the one you think it is, the parser is just doing it's best and reporting where it found an error.
There's one more issue:
$res = $this->my_model->getAll();
Should be:
$res = $this->my_model->get_all();
You have called your own function by the wrong name.