Discussion:
assign value from fetch array
ICCSI
2014-05-19 23:57:51 UTC
Permalink
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign the
array the value to echo the result.

I think I am missing something between array and variable.

Your help and information is great appreciated,

Iccsi,


$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
Ricardo R Bonfim
2014-05-20 03:03:06 UTC
Permalink
If I got it correctly, you're using PDO and using Statement fetchAll
method, which would return an array of arrays.

Your variable $row would fit best as $rows if you know what I mean...
try to iterate over the return of fetchAll and give it a print to see
how it looks like.
Post by ICCSI
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign the
array the value to echo the result.
I think I am missing something between array and variable.
Your help and information is great appreciated,
Iccsi,
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
--
Att,
____________________________________________________
Ricardo R Bonfim
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
ICCSI
2014-05-20 21:31:09 UTC
Permalink
Thanks for the message,
Yes, I use PDO to create connection and and use fetchAll to get data.
The print result and iterate work, but I need pass the value to calling
function from jQuery.
I tried to use fetch which still returns an array.

I want to learn how I can access array and assign an array value to my
variable and send back to calling function.

Thanks again for helping,


Regards,


Iccsi,
Post by Ricardo R Bonfim
If I got it correctly, you're using PDO and using Statement fetchAll
method, which would return an array of arrays.
Your variable $row would fit best as $rows if you know what I mean...
try to iterate over the return of fetchAll and give it a print to see
how it looks like.
Post by ICCSI
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign the
array the value to echo the result.
I think I am missing something between array and variable.
Your help and information is great appreciated,
Iccsi,
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
--
Att,
____________________________________________________
Ricardo R Bonfim
Kumar Saurabh Sinha
2014-05-22 14:46:13 UTC
Permalink
Sorry, although i was not able to understand problem very much..
But if it is about transferring a php array to jquery...
Then you can json encode the array in php and assign it dynamically to a
jquery variable and then use parseJson in jquery to get the values in jquery

I hope this might help..

Thanks
Kumar
Post by ICCSI
Thanks for the message,
Yes, I use PDO to create connection and and use fetchAll to get data.
The print result and iterate work, but I need pass the value to calling
function from jQuery.
I tried to use fetch which still returns an array.
I want to learn how I can access array and assign an array value to my
variable and send back to calling function.
Thanks again for helping,
Regards,
Iccsi,
Post by Ricardo R Bonfim
If I got it correctly, you're using PDO and using Statement fetchAll
method, which would return an array of arrays.
Your variable $row would fit best as $rows if you know what I mean...
try to iterate over the return of fetchAll and give it a print to see
how it looks like.
Post by ICCSI
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign
the
Post by Ricardo R Bonfim
Post by ICCSI
array the value to echo the result.
I think I am missing something between array and variable.
Your help and information is great appreciated,
Iccsi,
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
--
Att,
____________________________________________________
Ricardo R Bonfim
Shawn McKenzie
2014-05-22 15:52:48 UTC
Permalink
The rows are indexed by number and then contain an array of the columns.
So this would work, but you probably want to loop over $rows:

$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
$name = $rows[0]['MyID'];
echo $name;

When you fetch only one row it is just an array of columns:

$row = $sth->fetch(PDO::FETCH_ASSOC);
$name = $row['MyID'];
echo $name;

-Shawn
Post by ICCSI
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign the
array the value to echo the result.
I think I am missing something between array and variable.
Your help and information is great appreciated,
Iccsi,
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
ICCSI
2014-05-24 17:56:30 UTC
Permalink
Thanks a million for helping and information,

Regards,


Iccsi,
Post by Shawn McKenzie
The rows are indexed by number and then contain an array of the columns.
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
$name = $rows[0]['MyID'];
echo $name;
$row = $sth->fetch(PDO::FETCH_ASSOC);
$name = $row['MyID'];
echo $name;
-Shawn
Post by ICCSI
I have following code to fetch data from database.
I am able to print the array, but I got HTTP 500 error when I assign the
array the value to echo the result.
I think I am missing something between array and variable.
Your help and information is great appreciated,
Iccsi,
$row = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
print("\n");
$name = "{$row['MyID']}";
echo $name;
Loading...