PDO数组获取时出现”Array to string conversion”错误。

huangapple go评论77阅读模式
英文:

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?

&lt;?php $data = [&#39;first_name&#39; =&gt; &#39;ben&#39;] ?&gt;
&lt;?php $sql = &quot;INSERT INTO names (first_name) values (?);&quot; ?&gt;

&lt;?php $statement = $pdo-&gt;prepare($sql); ?&gt;
&lt;?php $statement-&gt;execute([$data]); ?&gt;

答案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 = &quot;INSERT INTO table (col1, col2) values (?, ?)&quot;;
$data = [&#39;value for col1&#39;, &#39;value for col2&#39;];

Note the values must be in the same order that they're going to be used:

$data = [&#39;value for col2&#39;, &#39;value for col1&#39;]; // This won&#39;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 = &quot;INSERT INTO table (col1, col2) values (:col1, :col2)&quot;;
$data = [&#39;col1&#39; =&gt; &#39;value for col1&#39;, &#39;col2&#39; =&gt; &#39;value for col2&#39;];

The order of these now does not matter because they're keyed by the array name instead of the position:

$data = [&#39;col2&#39; =&gt; &#39;value for col2&#39;, &#39;col1&#39; =&gt; &#39;value for col1&#39;]; // 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 = [&#39;ben&#39;];
$sql = &quot;INSERT INTO names (first_name) values (?);&quot;;
$statement = $pdo-&gt;prepare($sql);
$statement-&gt;execute($data);

Or named parameters and an associative array:

$data = [&#39;first_name&#39; =&gt; &#39;ben&#39;];
$sql = &quot;INSERT INTO names (first_name) values (:first_name);&quot;;
$statement = $pdo-&gt;prepare($sql);
$statement-&gt;execute($data);

huangapple
  • 本文由 发表于 2023年2月14日 07:48:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定