Home >Backend Development >PHP Tutorial >php中mysql查询用变量替换怎么弄

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

WBOY
WBOYOriginal
2016-06-23 13:17:021000browse

各位大神们
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, ''张三,'李四,'王五')";

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn