PDO fetchAll 数组转为一维
这可能是一个简单的问题,但我很难理解如何解决它。我有一个表单,允许用户选择“自定义”或“所有”员工”来分配工作。
如果选择自定义,用户通过单击每个复选框来选择员工,然后我将它们插入到工作表中。这会产生下面的数组(3,1,10是员工ID)
Array
(
[0] => 3
[1] => 1
[2] => 10
)
如果选择“所有员工”,我首先查询一条select语句从员工表中获取所有员工ID,然后将它们插入到工作表中,与然而,这会产生数组:
Array
(
[0] => Array
(
[staffID] => 1
[0] => 1
)
[1] => Array
(
[staffID] => 23
[0] => 23
)
[2] => Array
(
[staffID] => 26
[0] => 26
)
[3] => Array
(
[staffID] => 29
[0] => 29
)
)
如何将上面的数组转换为显示的第一个数组?
我使用下面的代码查询数据库以获取所有员工 ID,然后插入它们。
$select = $db->prepare("SELECT staffID FROM staff");
if($select->execute())
{
$staff = $select->fetchAll();
}
for($i = 0; $i<count($staff); $i++)
{
$staffStmt = $db->prepare("INSERT INTO staffJobs (jobID, userID) VALUES (:jobID, :staffID)");
$staffStmt->bindParam(':jobID', $jobID, PDO::PARAM_INT);
$staffStmt->bindParam(':staffID', $staff[$i], PDO::PARAM_INT);
$staffStmt->execute();
}
第一个数组插入正常,但最后一个数组为 StaffID 插入零。 ?
有什么建议
吗
this may be a simple question but am struggling to understand how to solve it. I have a form which allows the user to select either "custom" or "all" staff" to assign to a job.
If custom is selected the user selects staff by clicking each checkbox, I then insert these into a jobs table. This produces the array below (3, 1, 10 are the staff IDs)
Array
(
[0] => 3
[1] => 1
[2] => 10
)
If "all staff" is selected, I first query a select statement to get all the staff ID's from the staff table, and then insert these into the job table the same as above. However this produces the array :
Array
(
[0] => Array
(
[staffID] => 1
[0] => 1
)
[1] => Array
(
[staffID] => 23
[0] => 23
)
[2] => Array
(
[staffID] => 26
[0] => 26
)
[3] => Array
(
[staffID] => 29
[0] => 29
)
)
How can I convert the array above to the first array shown?
I'm using the code below to query the database to get all the staff ID's and then inserting them.
$select = $db->prepare("SELECT staffID FROM staff");
if($select->execute())
{
$staff = $select->fetchAll();
}
for($i = 0; $i<count($staff); $i++)
{
$staffStmt = $db->prepare("INSERT INTO staffJobs (jobID, userID) VALUES (:jobID, :staffID)");
$staffStmt->bindParam(':jobID', $jobID, PDO::PARAM_INT);
$staffStmt->bindParam(':staffID', $staff[$i], PDO::PARAM_INT);
$staffStmt->execute();
}
The first array inserts fine, however the last array inserts zeros for the staffID.
Any suggestions?
Thanks =).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看手册中的示例 2。在第一个查询中,您可以使用:
第二个数组将具有与第一个数组相同的形式。
Take a look at example 2 in the manual. In your first query you can use:
And your second array will have the same form as the first array.
如果您在
for
中print_r($staff[$i])
您可能会得到这意味着您应该使用
$staff[$i]['staffID' ]
代替。另一种选择,应该适用于您当前的代码,是使用
PDOStatement::fetchColumn()
而不是fetchAll()
。If you
print_r($staff[$i])
inside thefor
you would probably getwhich means you should use
$staff[$i]['staffID']
instead.The other alternative, which should work with your current code, is to use
PDOStatement::fetchColumn()
instead offetchAll()
.获取样式
您需要为 fetchAll 指定从 此链接
You need to give fetchAll a fetch style
From this link