CodeIgniter Forums
num_rows not working in ODBC Multiple DB object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: num_rows not working in ODBC Multiple DB object (/thread-64497.html)



num_rows not working in ODBC Multiple DB object - judaskit - 02-25-2016

I use ODBC DB
I found that I cannot use num_rows when it is a database object
For example:
$this->load->database('testing');
$sql = "SELECT top 10 * from [haha].[dbo].[Info]";

$query = $this->db->query($sql);
echo "1.";
echo $query->num_rows();
echo "<hr/>";


$main_db = $this->load->database('testing',true);
$query = $main_db->query($sql);
echo "2.";
echo $query->num_rows();
echo "<hr/>";

And the result would be:
1.10

2.0



Is it a CI bug or someone can give me some advice to fix it?


RE: num_rows not working in ODBC Multiple DB object - judaskit - 02-25-2016

One more problem
I found that if I just use $query->result(), I can get the result normally with both connection

but once I use $query->num_rows() in object $main_db
the $query will turn All Null

for example,
$query = $main_db->query($sql);
foreach ($query->result() as $row)
{
echo $row->a.",";
}
echo "<br/>";

$query = $main_db->query($sql);
echo $query->num_rows();
foreach ($query->result() as $row)
{
echo $row->a.",";
}


and result would be:
a,b,c,d,e,f,g,h,i,j,
0
^(this line supposed to run foreach too but nothing)


RE: num_rows not working in ODBC Multiple DB object - kilishan - 02-26-2016

While it's possible that's a bug in the ODBC driver, the current best practice is to NOT use num_rows. As you said, you can get the answer just by calling result(), row() or any of their siblings. The result* methods will always return an empty array IIRC, if there's no results, while the row* methods will always return null.