1

I am trying to create a single array from a db table that has few rows like:

--------------
id   |   name
--------------
1    | value 1
2    | value 2
3    | value 3
--------------

I want the created array to be as simple as possible like value1,value2,value3

I am using this function

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'];
    }

Is there a simple way to do it, perhaps with another PDO Mode like for example is done with PDO::FETCH_UNIQUE, or a should take a different approach?

UPDATE - This way it worked (thanks to @u_mulder)

function getAll()
{
    $sql = "SELECT name FROM " . $this->table_name . "  ORDER BY id";
    $prep_state = $this->db_conn->prepare($sql);
    $prep_state->execute();

    $names = $prep_state->fetchAll(PDO::FETCH_COLUMN, 0); 
    return(implode(",",$names));

}
5
  • 1
    What does Create single array mean here? Commented Aug 27, 2018 at 16:09
  • Possible duplicate of How to use PDO to fetch results array in PHP? Commented Aug 27, 2018 at 16:12
  • single dimension... like array(value1,value2,value3); Commented Aug 27, 2018 at 16:13
  • 2
    Why not use fetchAll instead of fetch since you want to return all rows? Commented Aug 27, 2018 at 16:15
  • Yes, fetchAll did it. Thanks Commented Aug 27, 2018 at 20:03

3 Answers 3

3

What you need is PDO::FETCH_COLUMN mode:

$sql = "SELECT name FROM " . $this->table_name . "  ORDER BY id";
$prep_state = $this->db_conn->prepare($sql);
$prep_state->execute();

// 0 indicates first column of result
$names = $prep_state->fetchAll(PDO::FETCH_COLUMN, 0); 
print_r($names);
// then do what you want.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It worked. instead of print_r i just added return(implode(",",$names));
1

if youe want all the name in an array you could try group_concat and 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;
}

Comments

1

Use the fetchAll method which returns an array, and then just return your array in a variable that you can use.

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
    }

Then to get the values out of this array.

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

The key will be the id and the value will be the name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.