CodeIgniter Forums
Syntax in function does not work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Syntax in function does not work (/showthread.php?tid=398)



Syntax in function does not work - lkudlacek - 11-29-2014

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


RE: Syntax in function does not work - Rufnex - 11-29-2014

You have to fetch a result back from you query. Look at the documentation herer:

http://www.codeigniter.com/userguide3/database/results.html


RE: Syntax in function does not work - lkudlacek - 11-29-2014

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.


RE: Syntax in function does not work - Rufnex - 11-29-2014

You should post your complete example so we can see how you use your function (method) in your class.


RE: Syntax in function does not work - lkudlacek - 11-29-2014

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


RE: Syntax in function does not work - Rufnex - 11-29-2014

Where do you call this function. You have to put it in a Model or in a Controller. Did you do this?


RE: Syntax in function does not work - lkudlacek - 11-29-2014

(11-29-2014, 04:50 PM)Rufnex Wrote: Where do you call this function. You have to put it in a Model or in a Controller. Did you do this?

The page is defined in Controller.


RE: Syntax in function does not work - Rufnex - 11-29-2014

Ok, have you loaded the database library in you config or in your controller?


RE: Syntax in function does not work - lkudlacek - 11-29-2014

(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');


RE: Syntax in function does not work - Rufnex - 11-29-2014

You have called the row() method wrong, try this:

PHP Code:
function test() {
  
$test $this->db->query("SELECT name FROM users");
  
$name $test->row()->name;
  return 
$name;