Solved

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

Posted on 2013-12-28
4
403 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 82

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 82

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server R2 Stored procedure performance 8 33
How to convert sql code to access query 8 55
SQL query 4 29
sql sentence 2 12
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now