0

I have following function which returns multiple rows of data, but it is returning separate arrays:

   function getAllDataByID($uid)
   {
   try {  

            $conn = DB::db_connect();
            $sql = "Select id from tbl1 where uid =:uid";
            $query = $conn->prepare($sql);
            $query->execute(array(':uid'=>$uid));
            $query->setFetchMode(PDO::FETCH_ASSOC);
            return $query;
        }
        catch (Exception $e) {
            return $e->getMessage();
        }   
   }

output:

array(1) {
  ["id"]=>
  string(2) "22"
}
array(1) {
  ["id"]=>
  string(2) "21"
}
array(1) {
  ["id"]=>
  string(2) "18"
}

Whereas I want it to be a nested array.

3
  • 2
    What do you mean by nested array? Do you have an example? Commented Apr 13, 2016 at 19:34
  • Possible duplicate of How to create a nested array out of an array in PHP Commented Apr 13, 2016 at 19:43
  • Basically, nested means i can access the array like $arr['id'][0],$arr['id'][1], $arr['id'][2] i want to print all the result with loop and with this seperate array's i am not able to do it Commented Apr 13, 2016 at 19:50

2 Answers 2

0

Sounds like you want to use PDO::FETCH_COLUMN, which will return tuples

Sign up to request clarification or add additional context in comments.

Comments

0

I am using my custom Database class for querying, inserting, updating and deleting in short working with my database and it works flawlessly. You do not have to use it but worth check out line 85 while ($row = $this->statement->fetch(PDO::FETCH_ASSOC)) where I append the row to existing array which will be used to return data in a way you need.

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.