Php 内爆函数的条件
我正在我的阵列上进行内爆:Php 内爆函数的条件,php,arrays,implode,Php,Arrays,Implode,我正在我的阵列上进行内爆: $values=“(”。内爆(“,”,$array)。“)” 它给了我这样的结果: $placeholders = implode(',', array_fill(0, count($data), '?')); ('abc','123','apple123','hello','345') 我需要它如下所示,所以数字不会用引号括起来: ('abc',123',apple123',hello',345) 问题是mysql数据库不希望正确导入数据。它将数字视为字符串,并为
$values=“(”。内爆(“,”,$array)。“)”代码>
它给了我这样的结果:
$placeholders = implode(',', array_fill(0, count($data), '?'));
('abc','123','apple123','hello','345')
我需要它如下所示,所以数字不会用引号括起来:
('abc',123',apple123',hello',345)
问题是mysql数据库不希望正确导入数据。它将数字视为字符串,并为它们提供最大可能的int值,因此导入的值与我的值不匹配
有没有办法添加更多的指令来内爆,这样它就可以识别什么是数字,什么不是数字?正如@GrafiCode的评论中提到的,你不能做你要求的事情
一个快速的解决方法是利用json\u encode
的行为将整数值保留为不带引号(如预期的那样):
然而:
看起来您正在动态地构建查询。如果你要做这样的事情:
$placeholders = implode(',', array_fill(0, count($data), '?'));
然后,您可以使用参数化查询/预处理语句,这些查询/预处理语句更加安全:
$q = $pdo->prepare("INSERT INTO people VALUES ({$placeholders})");
$q->execute($data);
// This is also possible in MySQLi and other database APIs, not just PDO.
// The string would look like this: INSERT INTO people VALUES (?,?,?,?,?)
// Every question mark is replaced by one value from the array
// when the query is executed.
您在构建查询后发送(动态)数据,无需担心引用
准备好的语句也是防止SQL注入的一种很好的方法。正如@GrafiCode的评论中提到的,你不能做你要求的事情
一个快速的解决方法是利用json\u encode
的行为将整数值保留为不带引号(如预期的那样):
然而:
看起来您正在动态地构建查询。如果你要做这样的事情:
$placeholders = implode(',', array_fill(0, count($data), '?'));
然后,您可以使用参数化查询/预处理语句,这些查询/预处理语句更加安全:
$q = $pdo->prepare("INSERT INTO people VALUES ({$placeholders})");
$q->execute($data);
// This is also possible in MySQLi and other database APIs, not just PDO.
// The string would look like this: INSERT INTO people VALUES (?,?,?,?,?)
// Every question mark is replaced by one value from the array
// when the query is executed.
您在构建查询后发送(动态)数据,无需担心引用
Prepared语句也是防止SQL注入的一种很好的方法。您必须像应该使用Prepared语句一样通过arraySounds进行迭代,但您只展示了如何生成字符串。如果这是构建一个INSERT语句,那么首先要有一个循环,您必须通过arraySounds进行迭代,就像您应该使用准备好的语句一样,但您只展示了如何生成字符串。如果这是构建INSERT语句,请在