我有问题 当我从数据库中获取记录时
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"));
它告诉我所有人
有谁知道为什么会这样?
答案 0 :(得分: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"));