问问题
2577 次
3 回答
3
array_map()
lets you call a function on every element of an array.
$tag_array = array_map ( function($var) {
return "'" . mysql_real_escape_string($var) . "'";
}, $tag_array );
But I suggest you use prepared statements (PDO or mysqli instead of the soon deprecated mysql_
functions).
于 2012-06-06T10:18:57.643 回答
2
Update
Misread the earlier question.
You have to escape each individual item in your array before you use it in the query:
function apply_quotes($item)
{
return "'" . mysql_real_escape_string($item) . "'";
}
$list = array('3D', '瑪丹娜','電影','情報','大獨裁');
$list = array_map('apply_quotes', $list);
Then, to construct the query:
$query = "select * FROM news WHERE upordown = 1 AND tagkey IN (" .
join(',', $list) .
") AND id <> $id ORDER BY udate DESC";
If all your items are inside a string instead of an array, you have to turn it into an array first:
$list = explode(',', '3D,瑪丹娜,電影,情報,大獨裁');
I'm assuming that you're using mysql_
functions; if not, let me know :)
于 2012-06-06T10:29:58.707 回答
-1
Your question is not clear, but try like this ;
$tag_array = array($line["tagkey"]);
$tag_str = implode(" ", array($line["tagkey"]));
echo $tag_str;
$query = "select * FROM news WHERE upordown = 1 AND tagkey IN ('".$tag_str."') AND id <> '".$id."' ORDER BY udate DESC ";
于 2012-06-06T10:16:50.277 回答