我的函数是这样的
function all_products($where, $condition, $where2, $condition2) {
$query = "SELECT *
FROM `product`
WHERE
".mysql_real_escape_string($where)." = '".mysql_real_escape_string($condition)."'
and
".mysql_real_escape_string($where)." = '".mysql_real_escape_string($condition)."'
";
$query_run = mysql_query($query);
return $query_run;
}因此,每当我试图使用这个函数从db获取数据时,它都会返回行,即使只有$where和$condition为真,$where2和$condition2为假。
发布于 2014-01-24 05:48:28
您可以支持可变长度的条件。
function all_products(array $assoc) {
foreach ($assoc as $key => $value) {
$pairs[] = sprintf(
"`%s` = '%s'",
addcslashes($key, '`'),
mysql_real_escape_string($value)
);
}
$sql = empty($pairs) ?
'SELECT NULL LIMIT 0' :
'SELECT * FROM `product` WHERE ' . implode(' AND ', $pairs);
return mysql_query($sql);
}但是,所有的mysql_*函数都已经是不推荐使用的。我强烈建议您迁移到PDO或mysqli。
https://stackoverflow.com/questions/21319743
复制相似问题