Welcome Guest, Not a member yet? Register   Sign In
Cannot make num_rows work
#1

[eluser]huskyfritz[/eluser]
Hello guys, I am newbie in codeigniter and I am developing a simple site using this framework. Well I am trying to use num_rows to get all records in an array but still no luck.

here's my codes:

Code:
$query_array = $this->db->query('SELECT STATEMENT');
    die($query_array->num_rows());

Is it correct? Is there any simpliest way to achieve this?

Thanks much! Smile
#2

[eluser]Beginers[/eluser]


num_rows will just get the number of rows affected not the value it contains

use

Code:
$query_array->result_array();
instead of
Code:
$query_array->num_rows()

to get the desired output you want.
#3

[eluser]huskyfritz[/eluser]
your suggestion did not work.. when I tried
Code:
$query_array = $this->db->query("SQL HERE");
   die($query_array->result_array());

it only displays "Array" word..

Thanks anyway Smile
#4

[eluser]Beginers[/eluser]
to test if it really get the values use

Code:
print_r($query_array->result_array());
#5

[eluser]huskyfritz[/eluser]
Yes, lots of records.. My code using num_rows is same as it showed in codeigniter documentation.

The documentation says like this:
Code:
$query = $this->db->query('SELECT * FROM my_table');
   echo $query->num_rows();

and here's my code:
Code:
$query_array = $this->db->query('SELECT STATEMENT');
  die($query_array->num_rows());

do u know what's wrong with my code?

Thanks
#6

[eluser]CroNiX[/eluser]
try echoing $query->num_rows() and then die() right after.

BTW: num_rows() will be an integer that tells the number of rows your query returned. It's not the actual results.
#7

[eluser]huskyfritz[/eluser]
Thanks CroNiX.

Its working fine now.
#8

[eluser]Beginers[/eluser]
of course the data will not display because you did not specify the fields that you want to display:
try to display it all using foreach loop
Code:
$query = $this->db->query('SELECT * FROM my_table');

foreach ($query->result_array() as $row)
{
   echo $row['fieldname'];
}




Theme © iAndrew 2016 - Forum software by © MyBB