php中mysql查询用变量替换怎么弄
程序员文章站
2022-05-30 17:33:23
...
各位大神们
php函数里想把变量替换到sql里头,$sql = "select * form t where name in ($names)"; $names 是个以逗号分隔的字符串,这样有问题吗
$names值为“张三,李四,王五”,真正mysql里in 里面的字符串是以单引号分隔的。
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, ''张三,'李四,'王五')";