Welcome Guest, Not a member yet? Register   Sign In
get() not returning any rows
#1

[eluser]jamojo[/eluser]
Hello Everyone,

I am using the function below but did not return any rows. I tried to do
Code:
$query->num_rows()
but it has null values. I got the last query using
Code:
$this->db->last_query(),
used this in the database and there are results. Am I missing a setup in CI? Looking forward to your reply.


Code:
function search($conditions=NULL,$tablename="",$orderby='id', $order='DESC',$limit=1000000,$offset=0)
{
  if($tablename=="")
  {
   $tablename = $this->table;
  }
  if($conditions != NULL)
   $this->db->where($conditions);
  if ( $orderby !== "" and $order !== "")
   $this->db->order_by($orderby, $order);
  $query = $this->db->get($tablename,$limit,$offset=0);
return $query->result_array();
}
#2

[eluser]Karman de Lange[/eluser]
Below should work, Just 1 thing, If you want to use pagination, then this will only return the num of rows in the current result with limited by the limit statement.

you can either do another query counting all rows without the limit statment (as per standard pagination tutorials) or you can use SQL_CALC_FOUND_ROWS which only use 1 query todo both reducing the load on server a bit. . I extended the default mysql drivers to have option for this so shout if you want the code.

Code:
$query = $this->db->get($tablename,$limit,$offset=0);
$num_rows = $query->num_rows();
$rows = $this->db->result_array();
return array('num_rows'=>$num_rows,'rows'=>$row);
#3

[eluser]jamojo[/eluser]
Thanks for the reply.

Actually, I am having trouble with a simple function below. There is no results from
Code:
get()
.

Code:
function test()
  {
    $data = array();
    $query = $this->db->get( "page" );
    echo "COUNT:".$query->num_rows();
    if ( $query->num_rows() > 0 )
    {
      $data = $query->result_array();
    }
    return $data;
  }

I extended the CI Model,
Code:
MY_Model.php
and placed it in application/core folder. CI is running is a sub-domain where it is installed in a sub-folder.

Below is my
Code:
database.php

Code:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'dbuser';
$db['default']['password'] = 'dbuser';
$db['default']['database'] = 'dbase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

I hope my info helps.
#4

[eluser]Karman de Lange[/eluser]
Enable your db_debug .. If there any errors with your settings it will be shown.

Also, i you manual run the sql query from $this->db->last_query() , do you get any results?
#5

[eluser]jamojo[/eluser]
I encountered A Database Error Occurred Unable to connect to your database server using the provided settings. Filename: core/Loader.php when I enabled db_debug.

I was able to get results from $this->db->last_query() manually.
#6

[eluser]Karman de Lange[/eluser]
check your username/password and table name .. that combo must be wrong somewhere
#7

[eluser]jamojo[/eluser]
I already check it, I also made a simple PHP file to check the connection.

Code:
$link = mysql_connect("localhost", "dbuser", "dbuser");
mysql_select_db("dbase", $link);
$result = mysql_query("SELECT * FROM page", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysql_close($link);

Thanks for the help. I'm stuck in limbo now. Sad
#8

[eluser]Karman de Lange[/eluser]
Ok, the below should dump all the info regarding the DB connection.. the db user/pass etc will be int here as well as last query..
If you just get db_connect failure maybe disable the debugging again and try again.

If still don't give you idea what going on.. can you maybe drop me your full source tree (or the parts not working) on email or something so I can check out myside.

Code:
function test()
  {
    $data = array();
    $query = $this->db->get( "page" );
echo '<pre>' . print_r($this->db, true) . '</pre>';
  }




Theme © iAndrew 2016 - Forum software by © MyBB