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.
- never use unchecked/unvalidated request values, not in examples!
- use query builder.
- reduce coupling a) setting database in constructor, done in models, , b) retrieve id in controller.
- you try fields (*) multiple tables, have column names in common. not work.
- 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
Post a Comment