计算从搜索查询返回的行数

时间:2014-03-11 09:05:21

标签: php mysql sql search pdo

我正在尝试设置管理搜索功能,该功能从搜索字段中获取值,将查询发送到包含名为“data”的表的SQL数据库和名为“tag”的表,并返回两者结果和行数。结果完全正常,但结果的数量没有。这是相关的代码:

    $query="SELECT * FROM data";
    $where="";
    $params=array();
    if(!empty($idsearch)){
        $where.=" id = ? AND";
        $params[]=$idsearch;
    }
    if(!empty($approvedsearch)&&$approvedsearch!=="A"){
        $where.=" approved = ? AND";
        $params[]=$approvedsearch;
    }
    if(!empty($namesearch)){
        $where.=" name LIKE ? AND";
        $params[]=$namesearch."%";
    }
    if(!empty($emailsearch)){
        $where.=" email LIKE ? AND";
        $params[]=$emailsearch."%";
    }
    if(!empty($fileurlsearch)){
        $where.=" (url LIKE ? OR fileid LIKE ?) AND";
        $params[]="%".$fileurlsearch."%";
        $params[]="%".$fileurlsearch."%";
    }
    if(!empty($datesearch)){
        $where.=" timedate LIKE ? AND";
        $params[]=$datesearch."%";
    }
    if(!empty($tagsearch)){
        $query.=" INNER JOIN tags ON tag.data_id = data.id";
        $tags=explode(" ",$tagsearch);
        $tagMarks=substr(implode(array_pad(array(),count($tags),"?, ")),0,-2);
        $where.=" tag.name IN (".$tagMarks.") AND";
        $params=array_merge($params,$tags);
    }
    if($where !== ""){
        $where=substr($where,0,-4);
        $query.=" WHERE ".$where/*." LIMIT ".$lowlim.", ".$uplim*/;
    }
    $countQuery="SELECT count(DISTINCT data.id) as count FROM data ".$where;
    $countQuery.=" INNER JOIN tags ON tag.data_id = data.id";
    $countQuery.=" WHERE ".$where;
    $countQuery.=" GROUP BY data.id";
    $countQuery=$con->prepare($countQuery);
    $countQuery->execute($params);
    $rowResult=$countQuery->fetchAll(PDO::FETCH_ASSOC);
    $totalRows=$rowResult[0]['count'];
    $query.=" GROUP BY data.id";
    $query=$con->prepare($query);
    $query->execute($params);
    $results=$query->fetchAll(PDO::FETCH_ASSOC);
    $datas=array();
    foreach($results as $result){
        $result['tag']=array();
        $datas[$result['id']]=$result;
    }
    $dataIds=array_keys($datas);
    $dataMarks=substr(implode(array_pad(array(),count($dataIds),"?, ")),0,-2);
    $query=$con->prepare("SELECT * FROM tag WHERE data_id IN (".$dataMarks.")");
    $query->execute($dataIds);
    $results=$query->fetchAll(PDO::FETCH_ASSOC);
    foreach($results as $result){
        $dataId=$result['data_id'];
        $datas[$dataId]["tag"][]=$result;
    }
    foreach($datas as $data){
        $tags=array();
        foreach($data['tag'] as $tag){
            $tags[]=$tag['tag_name'];
        }
?>
<tr>
    <td><?=$data['id']?></td>
    <td><?=$data['approved']?></td>
    <td><?=$data['name']?></td>
    <td><?=$data['email']?></td>
    <td><?=$data['fileid']?><?=$data['url']?></td>
    <td><?=$data['notes']?></td>
    <td><?=$data['timedate']?></td>
    <td><?=implode(", ",$tags)?></td>
</tr>    
<?php }?>

我认为countQuery是一个有问题的问题,但是引发的错误是Notice: Undefined offset: 0 in /var/www/admin.php on line 97$totalRows=$rowResult[0]['count'];

1 个答案:

答案 0 :(得分:0)

我想我发现你的mysql语法错误:

您要将$where部分添加两次到您的查询中,而且您似乎在这里有一点拼写错误tags ON tag.data_id

$countQuery="SELECT count(DISTINCT data.id) as count FROM data ".$where;
$countQuery.=" INNER JOIN tags ON tag.data_id = data.id";
$countQuery.=" WHERE ".$where;
$countQuery.=" GROUP BY data.id";

尝试修复它:

$countQuery="SELECT count(DISTINCT data.id) as count FROM data ";
$countQuery.=" INNER JOIN tags ON tags.data_id = data.id";
$countQuery.=" WHERE ".$where;
$countQuery.=" GROUP BY data.id";

修改

请在PDO中设置错误处理如下:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

使用此设置将引发异常。我也发现了拼写错误。见上文(tagstag.data_id

编辑2

您收到以下例外情况:

  

致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'GROUP BY附近使用正确的语法   /var/www/admin.php:96中的data.id'在第1行'堆栈跟踪:#0   /var/www/admin.php(96):PDOStatement-&gt;执行(数组)#1 {main}抛出   在第96行的/var/www/admin.php中

所以GROUP BY无效,这是有道理的,因为你只算数。删除它:

$countQuery="SELECT count(DISTINCT data.id) as count FROM data ";
$countQuery.=" INNER JOIN tags ON tags.data_id = data.id";
$countQuery.=" WHERE ".$where;