PDO使用fetchall阵列一维 [英] PDO fetchAll array to one dimensional

查看:186
本文介绍了PDO使用fetchall阵列一维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但我在努力了解如何解决它。我有一个形式,它允许用户选择自定义或所有员工分配到的工作。

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.

如果选择自定义的用户通​​过点击每个复选框选择的工作人员,我然后将这些目标变为一个工作表中。这将产生下面的阵列(3,1,图10是工作人员的ID)

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
)

如果选择了全体员工,我第一次查询的SELECT语句来获得全体员工的ID从人员表,然后插入到这些工作表与上面相同。然而,这产生的数组:

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?

我使用的是code以下查询数据库得到全体员工的ID然后把它们插入。

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();              

}

第一个数组插入正常,但是最后阵列插入零为STAFFID是。

The first array inserts fine, however the last array inserts zeros for the staffID.

有什么建议?

感谢=)。

推荐答案

例子2 在说明书中无。在第一个查询你可以使用:

Take a look at example 2 in the manual. In your first query you can use:

$staff = $select->fetchAll(PDO::FETCH_COLUMN, 0);

和您的第二个数组将具有相同形式的第一个数组。

And your second array will have the same form as the first array.

这篇关于PDO使用fetchall阵列一维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆