英文:
pdo array getting Array to string conversion error
问题
当我运行这段代码时,它应该将ben
存储在数据库中,但是它在first_name
列中显示Array
,并出现字符串转换错误。如何摆脱这个错误?
<?php $data = ['first_name' => 'ben'] ?>
<?php $sql = "INSERT INTO names (first_name) values (?);" ?>
<?php $statement = $pdo->prepare($sql); ?>
<?php $statement->execute([$data]); ?>
英文:
When I run this code it should store ben
in the database but, it says Array
in the first_name
column and it gives the string to conversion error. How would I get rid of the error?
<?php $data = ['first_name' => 'ben'] ?>
<?php $sql = "INSERT INTO names (first_name) values (?);" ?>
<?php $statement = $pdo->prepare($sql); ?>
<?php $statement->execute([$data]); ?>
答案1
得分: 2
PDO 有两种不同的参数绑定方式。第一种是位置绑定。在这种情况下,你传递给 execute()
的数组应该是一个索引数组,其中的值按照你希望它们绑定到问号的顺序排列:
$sql = "INSERT INTO table (col1, col2) values (?, ?)";
$data = ['col1的值', 'col2的值'];
请注意,这些值必须按照它们将要使用的顺序排列:
$data = ['col2的值', 'col1的值']; // 这是错误的顺序,不会起作用!
另一种(我认为更好的)方法是使用命名参数。在这种情况下,你需要使用一个关联数组,其中的键名与你的参数占位符相同:
$sql = "INSERT INTO table (col1, col2) values (:col1, :col2)";
$data = ['col1' => 'col1的值', 'col2' => 'col2的值'];
现在这些的顺序不再重要,因为它们是由数组名称而不是位置进行键控制的:
$data = ['col2' => 'col2的值', 'col1' => 'col1的值']; // 仍然有效!
你的问题(除了 @Sammitch 指出的额外数组包装问题之外)是你以一种不兼容的方式混合了这两种技术 —— 你使用了位置参数,但提供了一个关联数组。所以,在你的情况下,你要么使用位置参数和一个索引数组:
$data = ['ben'];
$sql = "INSERT INTO names (first_name) values (?);";
$statement = $pdo->prepare($sql);
$statement->execute($data);
或者使用命名参数和一个关联数组:
$data = ['first_name' => 'ben'];
$sql = "INSERT INTO names (first_name) values (:first_name);";
$statement = $pdo->prepare($sql);
$statement->execute($data);
英文:
PDO has two different ways to bind parameters. The first is positional. In this case, the array you pass to execute()
should be an indexed array, with values in the same order that you want them to bind to the question marks:
$sql = "INSERT INTO table (col1, col2) values (?, ?)";
$data = ['value for col1', 'value for col2'];
Note the values must be in the same order that they're going to be used:
$data = ['value for col2', 'value for col1']; // This won't work, wrong order!
The alternative (and in my opinion, superior) method is to use named parameters. Here, you need to use an associative array with a key named the same as your parameter placeholder.
$sql = "INSERT INTO table (col1, col2) values (:col1, :col2)";
$data = ['col1' => 'value for col1', 'col2' => 'value for col2'];
The order of these now does not matter because they're keyed by the array name instead of the position:
$data = ['col2' => 'value for col2', 'col1' => 'value for col1']; // Still good!
Your problem (in addition to the extra array wrap that @Sammitch pointed out) is that you have mixed these two techniques together in an incompatible way -- you're using positional parameters, but have provided an associative array. So, in your case, you either need to use positional parameters and an indexed array:
$data = ['ben'];
$sql = "INSERT INTO names (first_name) values (?);";
$statement = $pdo->prepare($sql);
$statement->execute($data);
Or named parameters and an associative array:
$data = ['first_name' => 'ben'];
$sql = "INSERT INTO names (first_name) values (:first_name);";
$statement = $pdo->prepare($sql);
$statement->execute($data);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论