当前位置:Gxlcms > PHP教程 > php中mysql查询用变量替换怎么弄

php中mysql查询用变量替换怎么弄

时间:2021-07-01 10:21:17 帮助过:39人阅读

各位大神们
php函数里想把变量替换到sql里头,$sql = "select * form t where name in ($names)"; $names 是个以逗号分隔的字符串,这样有问题吗
$names值为“张三,李四,王五”,真正mysql里in 里面的字符串是以单引号分隔的。


回复讨论(解决方案)

$names = '"张三','李四','王五'";
$sql = "select * form t where name in ($names)";

$ar = array("张三', '李四', '王五');
$names = join("'", $a);
$sql = "select * form t where name in ('$names')";

还可以
$sql = "select * form t where find_in_set(name, ''张三,'李四,'王五')";

人气教程排行