【问题标题】:mysql_num_rows() expects parameter 1 to be resource, boolean in joomla componentmysql_num_rows() 期望参数 1 是资源,joomla 组件中的布尔值
【发布时间】:2013-08-08 05:12:22
【问题描述】:

当我执行下面的函数时,查询执行成功,但我在查询结果上方收到警告:

mysql_num_rows() 期望参数 1 是资源,布尔值

我该如何解决这个问题?

public function retrieve()
{
    $id=JRequest::getVar('id');
    $db =JFactory::getDBO();
    $sql="select * 
          from 
            #__npco_car,#__npco_namayeshgah,#__npco_agahi
          where 
            #__npco_car.car_id='$id' and
            #__npco_namayeshgah.id_namayeshgah=#__npco_agahi.id_namayeshgah and 
            #__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 * 
              from 
                 #__npco_car,#__npco_useragahi,#__npco_user
              where 
                 #__npco_car.car_id='$id' and 
                 #__npco_user.id_user=#__npco_useragahi.id_user and 
                 #__npco_car.car_id=#__npco_useragahi.car_id
        ";
        $db->setQuery($sql);
        return $db->loadAssocList();
    }
}

【问题讨论】:

  • 尝试在$db =JFactory::getDBO(); 之后添加$query = $db->getQuery(true);。另外,我假设您使用的是 Joomla 2.5 或 3.x,因此在编写 SQL 查询时,我建议您坚持正确的编码标准;)
  • 查看此页面的右侧。看看有多少以前的问题被问到完全相同的问题。这意味着您运行了无效查询,但在尝试使用结果之前未检查错误。修复您的查询并添加一些错误处理。

标签: mysql joomla


【解决方案1】:

您的代码有几个问题。

  1. 从不使用未经检查/未经验证的请求值,甚至在示例中也不
  2. 使用查询生成器。
  3. 通过以下方式减少耦合:a) 在构造函数中设置数据库,这已在模型中完成,b) 在控制器中检索 id。
  4. 您尝试从多个表中获取所有字段 (*),这些表有一些共同的列名。那是行不通的。
  5. 查看 JOIN。

这将起作用:

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;
}

【讨论】:

    【解决方案2】:

    可能这是你的问题..

    如下更改您的查询

     $sql="select * 
              from 
                #__npco_car,#__npco_namayeshgah,#__npco_agahi
              where 
                #__npco_car.car_id='".$id."' and
                #__npco_namayeshgah.id_namayeshgah=#__npco_agahi.id_namayeshgah and 
                #__npco_car.car_id=#__npco_agahi.car_id
         ";
    
    
     $sql="select * 
                  from 
                     #__npco_car,#__npco_useragahi,#__npco_user
                  where 
                     #__npco_car.car_id='".$id."' and 
                     #__npco_user.id_user=#__npco_useragahi.id_user and 
                     #__npco_car.car_id=#__npco_useragahi.car_id
            ";
    

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多