Syntax in function does not work |
Hi,
I have the problem with CodeIgniter syntax in function. It works: $value = $this->db->query("SELECT * FROM `z_kinds`"); echo $value; It does not work: function value() { $value = $this->db->query("SELECT * FROM `z_kinds`"); return $value } I have to use only: function value() { $value = mysql_query("SELECT * FROM `z_kinds`"); } I want to use syntax $this->db->query and not mysql_query. What do I have wrongly? Thanks very much. L
You have to fetch a result back from you query. Look at the documentation herer:
http://www.codeigniter.com/userguide3/da...sults.html
I don`t have problem with query. It works me.
I have a problem with query in FUNCTION(). function test() { $query = $this->db->query("YOUR QUERY"); if ($query->num_rows() > 0) { $row = $query->row(); echo $row->title; echo $row->name; echo $row->body; } } The query without function() works.
You should post your complete example so we can see how you use your function (method) in your class.
Ok...so..for example does not work this:
function test() { $test = $this->db->query("SELECT name FROM users"); $name = $query->row()->name; return $name; } This example WORKS: function test() { $test = mysql_query("SELECT name FROM users"); $name = mysql_result($test, "0", "name"); return $name; } It does not work CodeIgniter syntax with $this->db->query. But ONLY IN FUNCTION(). Without function() it works. The error is: Fatal error: Using $this when not in object context in /folder/with/project/admin.php on line 247
Where do you call this function. You have to put it in a Model or in a Controller. Did you do this?
Ok, have you loaded the database library in you config or in your controller?
(11-29-2014, 04:54 PM)Rufnex Wrote: Ok, have you loaded the database library in you config or in your controller? I call database from this command on main page/file (header.php) and it works through all project: $this->load->database(); - only excepting function(). I changed in application/autoload.php this line: $autoload['libraries'] = array(); to $autoload['libraries'] = array('database');
You have called the row() method wrong, try this:
PHP Code: function test() { |
Welcome Guest, Not a member yet? Register Sign In |