1

我希望查询最初返回 c_id>0 的所有记录,然后根据通过文本框提供的后续条件进行过滤。但是,第一次访问该页面时,我没有打印任何记录。这是代码:

$ctitle = mysql_real_escape_string($_POST['ctitle']);
$csubject = mysql_real_escape_string($_POST['csubject']);
$creference = mysql_real_escape_string($_POST['creference']);
$cobjecttype = mysql_real_escape_string($_POST['cobjecttype']);
$cmaterial = mysql_real_escape_string($_POST['cmaterial']);
$ctechnic = mysql_real_escape_string($_POST['ctechnic']);
$cartist = mysql_real_escape_string($_POST['cartist']);
$csource = mysql_real_escape_string($_POST['csource']);

    $sql = "SELECT * FROM collections WHERE (
    c_id>0 AND
    `ctitle` LIKE '{$ctitle}' AND
    `csubject` LIKE '{$csubject}' AND
    `creference` LIKE '{$creference}' AND
    `cobjecttype` LIKE '{$cobjecttype}' AND
    `cmaterial` LIKE '{$cmaterial}' AND
    `ctechnic` LIKE '{$ctechnic}' AND
    `csource` LIKE '{$csource}' AND
    `cartist` LIKE '{$cartist}'
    )ORDER BY c_id DESC";

当我回显查询时,我会打印以下内容:

request "Could not execute SQL query" SELECT * FROM collections WHERE ( c_id>0 AND `ctitle` LIKE '' AND `csubject` LIKE '' AND `creference` LIKE '' AND `cobjecttype` LIKE '' AND `cmaterial` LIKE '' AND `ctechnic` LIKE '' AND `csource` LIKE '' AND `cartist` LIKE '' )ORDER BY c_id DESC

我应该从这里去哪里?

4

3 回答 3

0

)在和之间放一个空格ORDER BY

于 2013-04-10T06:38:57.093 回答
0

您不需要在 tow where 子句元素周围使用大括号。

于 2013-04-10T06:40:04.997 回答
0

好的...我想我明白了...尝试用 LIKE '%' 替换LIKE ' '

LIKE ''不起作用,因为它只显示空列。通过添加通配符(%),它将显示与所有内容匹配的所有行。但是,完全动态地构建查询,省略空的 WHERE 条件,这可能是一个更好、更简洁的解决方案。

于 2013-07-17T23:58:10.523 回答