Welcome Guest, Not a member yet? Register   Sign In
Syntax in function does not work
#1

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
Reply
#2

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

http://www.codeigniter.com/userguide3/da...sults.html

Reply
#3

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.
Reply
#4

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

Reply
#5

(This post was last modified: 11-29-2014, 04:46 PM by lkudlacek.)

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
Reply
#6

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

Reply
#7

(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.
Reply
#8

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

Reply
#9

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

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;


Reply




Theme © iAndrew 2016 - Forum software by © MyBB