mysql - mysql_num_rows() expects parameter 1 to be resource, boolean in joomla component -


when execute function below query execute warning above result query:

mysql_num_rows() expects parameter 1 resource, boolean

how fix this?

public function retrieve() {     $id=jrequest::getvar('id');     $db =jfactory::getdbo();     $sql="select *                         #__npco_car,#__npco_namayeshgah,#__npco_agahi                        #__npco_car.car_id='$id' ,             #__npco_namayeshgah.id_namayeshgah=#__npco_agahi.id_namayeshgah ,              #__npco_car.car_id=#__npco_agahi.car_id      ";     $db->setquery($sql);      $db->query();     $row = $db->getnumrows();      if($row == 1) {         return $db->loadassoclist();        } else {         $db = jfactory::getdbo();         $sql="select *                                  #__npco_car,#__npco_useragahi,#__npco_user                                 #__npco_car.car_id='$id' ,                   #__npco_user.id_user=#__npco_useragahi.id_user ,                   #__npco_car.car_id=#__npco_useragahi.car_id         ";         $db->setquery($sql);         return $db->loadassoclist();     } } 

your code has several issues.

  1. never use unchecked/unvalidated request values, not in examples!
  2. use query builder.
  3. reduce coupling a) setting database in constructor, done in models, , b) retrieve id in controller.
  4. you try fields (*) multiple tables, have column names in common. not work.
  5. have @ joins.

this work:

public function retrieve($id) {     $query = $this->_db->getquery(true);     $query->select('#__npco_car.*')->from(array('#__npco_car', '#__npco_namayeshgah', '#__npco_agahi'));     $query->where('#__npco_car.car_id = ' . (int) $id);     $query->where('#__npco_namayeshgah.id_namayeshgah = #__npco_agahi.id_namayeshgah');     $query->where('#__npco_car.car_id = #__npco_agahi.car_id');      $this->_db->setquery($sql);     $rows = $this->_db->loadassoclist();      if (empty($rows))     {             $query = $this->_db->getquery(true);         $query->select('#__npco_car.*')->from(array('#__npco_car, #__npco_useragahi, #__npco_user'));         $query->where('#__npco_car.car_id = ' . (int) $id);         $query->where('#__npco_user.id_user = #__npco_useragahi.id_user');         $query->where('#__npco_car.car_id = #__npco_useragahi.car_id');         $db->setquery($sql);         $this->_db->setquery($sql);         $rows = $this->_db->loadassoclist();     }      return $rows; } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -