mysql_num_rows()期望参数1是资源,给定字符串,错误

时间:2015-04-13 09:25:05

标签: php mysql

我已经阅读了很多关于这个确切问题的其他线程,但由于某种原因,我无法解决我的问题。真的需要一些帮助。

查询很好,我在phpmyadmin中测试了它,结果是:

  

MySQL返回一个空结果集(即零行)。 (查询了   0.0004秒。)

来源

    $items=array();

    $kwer="select distinct(d.id) as item, d.name, sd.date
        from sklad h
        inner join sent_item sd on sd.id_item = h.id
        inner join customers d on d.id=sd.id_customer
        where h.id=".$this->id." and sd.type=2
        order by sd.date, d.name asc";

    if($kwer === FALSE) {
        die(mysql_error()); // TODO: better error handling
    } else {

        if($kwer){
            $result = mysql_query($kwer);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
            }
            var_dump($result);
            //die;
            if (mysql_num_rows($kwer) != 0){

            $res=mysql_query($kwer);
                while($row=mysql_fetch_object($res)) {
                    $items[]=$row->items;
                }
            }
        }
    }
    return $items;

错误

resource(7) of type (mysql result) 
Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /test.php on line 284

修改

    $items=array();

    $kwer="select distinct(d.id) as item, d.name, sd.date
        from sklad h
        inner join sent_item sd on sd.id_item = h.id
        inner join customers d on d.id=sd.id_customer
        where h.id=".$this->id." and sd.type=2
        order by sd.date, d.name asc";

    if($kwer === FALSE) {
        die(mysql_error()); // TODO: better error handling
    } else {

        if($kwer){
            $result = mysql_query($kwer);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
            }
            var_dump($result);
            //die;
            if (mysql_num_rows($result) != 0){
                while($row=mysql_fetch_object($result)) {
                    $items[]=$row->items;
                }
            }
        }
    }
    return $items;

2 个答案:

答案 0 :(得分:1)

变化:

 if (mysql_num_rows($kwer) != 0){

 if (mysql_num_rows($result) != 0){

答案 1 :(得分:0)

你应该尝试这样的事情:

if (mysql_num_rows($result)!=0){