CodeIgniter Forums

Full Version: Cannot make num_rows work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

[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.

El Forum

[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

El Forum

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

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

El Forum

[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

El Forum

[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.

El Forum

[eluser]huskyfritz[/eluser]
Thanks CroNiX.

Its working fine now.

El Forum

[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'];
}