Solved

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

Posted on 2013-12-28
4
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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