php
fatal error in php
if(!empty($home) && !empty($title) && !empty($meta)) { function updcfg($condition,$status){ $db->query("UPDATE site_config SET status = '".$status."' WHERE condition = '".$condition."' "); } updcfg("home",$home); updcfg("title",$title); updcfg("meta",$meta); updcfg("news",$news); } the current code that you see above is not working and giving me this error : Fatal error: Call to a member function query() on a non-object in /home/main.php on line 20 I deleted the function.. and the code worked with no problems ! can anyone tell what is wrong with the code?
pass $db in the function. function updcfg($condition,$status,$db){ $db->query("UPDATE site_config SET status = '".$status."' WHERE condition = '".$condition."' "); } updcfg("home",$home,$db);
You should put the function outside of the if statement.
$db is not defined inside the updcfg function, and is not defined as global. So, there are three oprions: 1. use global keyword: function updcfg($condition,$status){ global $db; 2. pass $db to the function arguments : function updcfg($condition, $status, $db) 3. init $db inside the function (the way you do it)
please load database class before calling query method function updcfg($condition,$status){ $this->load->database(); $this->db->query("UPDATE site_config SET status = '".$status."' WHERE condition = '".$condition."' "); }
You need to initialize the $db variable. If you have initialized before is not alive inside the updcfg function, but you can pass the $db context as parameter: function updcfg($condition, $status, $db){ $db->query("UPDATE site_config SET status = '".$status."' WHERE condition = '".$condition."' "); } $db = new .... #Now you can do this updcfg("home",$home, $db); updcfg("title",$title,$db); updcfg("meta",$meta,$db); updcfg("news",$news,$db);
Related Links
Updating mysqli multiple columns error PHP [duplicate]
Creating model event not firing in observer
Loading data into the current view
Laravel doctrine only returning single attribute from findAll() query
How do I save multiple values of record in PHP to enter in database
Could not allow to take backup on akkeba backup in joomla 3.5.1
how to merge two different CI application to become one application?
php exec not getting whole output
Error when setting up VirtualHost in Wamp - The ServerName has syntax error in file httpd-vhosts.conf
PHP: Cannot read from a .txt file
How to upload image to server using angularjs?
My PHP file upload script hangs if file size > ~7.5MB, despite changing ini settings
PHP password_verify() not validating passwords
Why does my Wordpress Query not work on non Index Pages?
simplexml_load_file(): Unable to locate certificate CN
Laravel: Enforce unique rule if userid is different