我试图在子句
中使用mysql数组$result= $myDB->query("SELECT sum(total) as total FROM ".$myDB->prefix("mydata")." WHERE categoryname IN ('".$categoryname."') AND year='$year' AND stat_id='$stat_id'");
categoryname的当前输出是
('Cat1,Cat2,Cat3')
期望的输出
('Cat1','Cat2','Cat3')
我到目前为止尝试了它,但它不起作用
$categoryname_new = implode(',',$categoryname);
$result= $myDB->query("SELECT sum(total) as total FROM ".$myDB->prefix("mydata")." WHERE categoryname IN ('".$categoryname_new."') AND year='$year' AND stat_id='$stat_id'");
答案 0 :(得分:2)
我之前使用var qs = require("querystring");
var http = require("https");
var options = {
"method": "PATCH",
"hostname": "api.github.com",
"port": null,
"path": "/gists/GIST-ID-FORM-PREVIOUS-CALL",
"headers": {
"authorization": "token TOKEN-VALUE-FROM-PREVIOUS-CALL",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.write(qs.stringify(
{
"description": "updated gist",
"public": "true",
"files": {
"file1.txt": {
"content": "String file contents are now updated"
}
}
}));
req.end();
做过类似的事情,希望有所帮助:
array_map
上面的代码将迭代数组并修改每个元素以用$args = array_map(function($a) {
return sprintf("'%s'", $a);
}, $args);
$args = join(",", $args);
包围它。最后,我使用''
加入数组。
答案 1 :(得分:1)
天真的解决方案将是:
$array = ['Cat1', 'Cat2', 'Cat3'];
echo "'" . implode("','", $array) . "'";
但它可能会引入sql注入,因此您需要先在数组中正确转义数据
带有转义的单行示例:
echo "'" . implode("','", array_map('mysql_escape_string', $array)) . "'";
注意:{@ 1}}函数已弃用,您需要使用需要连接链接的mysql_*