Solved

PHP query ends up with an array inside of an array - please help!

Posted on 2013-12-28
4
416 Views
Last Modified: 2013-12-28
Hello all!  Thanks for taking a look at this...

I've set up a PDO connection string in PHP, which works fine.  I'm using the code below to run a simple query that should return a single line from the database.  The query appears to be working fine, but when I do a var_dump on the results, I get an array inside of an array.  It's like it's adding an extra "layer" of array.  Instead of just an array with the elements, it creates and array with one element that contains another array with the expected elements.  Please see the results below:

I'm hoping that you can help save the little hair I have left - it's coming out in clumps!

Here's the code, and the var-dump results are below it...

<?php  // Get existing values of case id if present in DB
$my_id = trim(strip_tags($_SESSION['my_id']));
try {
      $case_res = $pdo->query("SELECT m_city, m_state, m_country FROM user_info where my_id = '" .  $my_id . "'");

}catch (Exception $e){
      echo "Not Successfull:  Unable to read Case ID";
      }
      
echo "<pre>";      
//var_dump($case_res->fetchAll());      
var_dump($case_res->fetchAll(PDO::FETCH_ASSOC));
?>

Here are the results:

array(1) {
  [0]=>
  array(3) {
    ["m_city"]=>
    string(9) "hollywood"
    ["m_state"]=>
    string(2) "CA"
    ["m_country"]=>
    string(4) "Togo"
  }
}
0
Comment
Question by:ike1492
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39744166
According to the man page http://www.php.net/manual/en/pdostatement.fetchall.php , that's what it should do.  'fetchAll' is supposed to fetch ALL records in the select statement so you end up with an array of records which are in themselves arrays.  While you are selecting only one record, 'fetchAll' can be used to 'fetchAll' records in a table.  You can use 'foreach' to go thru them.

If you only want one record, you can use plain 'fetch' instead: http://www.php.net/manual/en/pdostatement.fetch.php
0
 

Author Comment

by:ike1492
ID: 39744197
Dave you are my hero.  Good thing this wasn't a snake!  I've been staring at it for two hours.  Thanks again!
0
 

Author Closing Comment

by:ike1492
ID: 39744198
A real lifesaver - thanks!

Ike
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39744224
You're welcome, glad to help and I avoid snakes.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question