0

从数据库获取记录时出现问题

mysql_select_db($database_Photo_con, $Photo_con);
$query_rsPhoto = "SELECT * FROM photographs WHERE visible = 1 ORDER BY id ASC";

这向我显示了所有可见 = 1 的记录

如果搜索

mysql_select_db($database_Photo_con, $Photo_con);
$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE caption LIKE %s OR caption_2 LIKE %s  AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));

它向我展示了一切有谁知道为什么会这样?

4

1 回答 1

2

这是因为运算符优先级(AND 的优先级高于 OR),所以应该使用括号。

$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE (caption LIKE %s OR caption_2 LIKE %s)  AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));

如果 GetSQLValueString 不返回带引号的字符串,则必须在查询中添加它们

$query_rsPhoto = sprintf("SELECT * FROM photographs WHERE (caption LIKE '%s' OR caption_2 LIKE '%s')  AND visible = 1 ", GetSQLValueString("%" . $colname_rsPhoto . "%", "text"),GetSQLValueString("%" . $colname_rsPhoto . "%", "text"));
于 2013-09-26T14:06:09.353 回答