Ike McMahon
asked on
PHP query ends up with an array inside of an array - please help!
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->fetc hAll());
var_dump($case_res->fetchA ll(PDO::FE TCH_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"
}
}
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[
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->fetc
var_dump($case_res->fetchA
?>
Here are the results:
array(1) {
[0]=>
array(3) {
["m_city"]=>
string(9) "hollywood"
["m_state"]=>
string(2) "CA"
["m_country"]=>
string(4) "Togo"
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A real lifesaver - thanks!
Ike
Ike
You're welcome, glad to help and I avoid snakes.
ASKER