Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP/PDO从MySQL创建单个数组_Php_Arrays_Pdo - Fatal编程技术网

使用PHP/PDO从MySQL创建单个数组

使用PHP/PDO从MySQL创建单个数组,php,arrays,pdo,Php,Arrays,Pdo,我正在尝试从一个db表中创建一个数组,该表只有几行,如下所示: -------------- id | name -------------- 1 | value 1 2 | value 2 3 | value 3 -------------- 我希望创建的数组尽可能简单,如value1、value2、value3 我正在使用这个函数 function getAll() { //select all data $sql = "S

我正在尝试从一个db表中创建一个数组,该表只有几行,如下所示:

--------------
id   |   name
--------------
1    | value 1
2    | value 2
3    | value 3
--------------
我希望创建的数组尽可能简单,如value1、value2、value3

我正在使用这个函数

function getAll()
    {
        //select all data
        $sql = "SELECT name FROM " . $this->table_name . "  ORDER BY id";

        $prep_state = $this->db_conn->prepare($sql);
        $prep_state->execute();

        $row = $prep_state->fetch(PDO::FETCH_ASSOC);
        $this->name = $row['name'];
    }
有没有一种简单的方法可以做到这一点,也许可以使用另一种PDO模式,例如使用PDO::FETCH_UNIQUE完成,或者a应该采用不同的方法

更新-多亏了@u_mulder,它才得以工作


如果你想把所有的名字都放在一个数组中,你可以尝试使用group_concat和explode

function getAll()
{
    //select all data
    $sql = "SELECT group_cancat(name) name FROM " . $this->table_name . "  ORDER BY id";

    $prep_state = $this->db_conn->prepare($sql);
    $prep_state->execute();

    $row = $prep_state->fetch(PDO::FETCH_ASSOC);

    // build an array from the concatenated  string pf names 
    $myArray = explode(',',  $row['name']);

    return $myArray;
}

如果你想把所有的名字都放在一个数组中,你可以尝试使用group_concat和explode

function getAll()
{
    //select all data
    $sql = "SELECT group_cancat(name) name FROM " . $this->table_name . "  ORDER BY id";

    $prep_state = $this->db_conn->prepare($sql);
    $prep_state->execute();

    $row = $prep_state->fetch(PDO::FETCH_ASSOC);

    // build an array from the concatenated  string pf names 
    $myArray = explode(',',  $row['name']);

    return $myArray;
}
您需要的是PDO::FETCH_列模式:

您需要的是PDO::FETCH_列模式:


使用fetchAll方法返回一个数组,然后在一个可以使用的变量中返回数组

function getAll()
    {
        //select all data
        $sql = "SELECT name FROM " . $this->table_name . "  ORDER BY id";

        $prep_state = $this->db_conn->prepare($sql);
        $prep_state->execute();

        $row = $prep_state->fetchAll(); //changed this
        $return $row; //and this
    }
然后从该数组中获取值

foreach($row as $key=>$value) {
//echo your results
}

键将是id,值将是名称。

使用fetchAll方法返回数组,然后只返回您可以使用的变量中的数组

function getAll()
    {
        //select all data
        $sql = "SELECT name FROM " . $this->table_name . "  ORDER BY id";

        $prep_state = $this->db_conn->prepare($sql);
        $prep_state->execute();

        $row = $prep_state->fetchAll(); //changed this
        $return $row; //and this
    }
然后从该数组中获取值

foreach($row as $key=>$value) {
//echo your results
}

键将是id,值将是名称。

这里创建单个数组意味着什么?单个维度可能重复。。。如arrayvalue1、value2、value3;既然要返回所有行,为什么不使用fetchAll而不是fetch呢?是的,fetchAll做到了。谢谢创建单个数组在这里意味着什么?可能是单个维度的重复。。。如arrayvalue1、value2、value3;既然要返回所有行,为什么不使用fetchAll而不是fetch呢?是的,fetchAll做到了。非常感谢你。成功了。我没有打印,而是添加了returninplade,,$names;谢谢。成功了。我没有打印,而是添加了returninplade,,$names;