By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Query returning array

P: n/a
I'm missing something very obvious, but it is getting late and I've
stared at it too long.

TIA for responses
I am writing a basic function (listed at the bottom of this post) that
returns data from a query into an array.

The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: 'foo' and 'bar'.

But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I'm wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was <em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.

--
Karl Groves

Apr 20 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Karl Groves wrote:
I'm missing something very obvious, but it is getting late and I've
stared at it too long.

TIA for responses
I am writing a basic function (listed at the bottom of this post) that
returns data from a query into an array.

The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: 'foo' and 'bar'.

But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I'm wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was <em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.
That's weird, your code seems to be working on my box.
The only thing that i can think of right now that would return that
result is when you return $rows instead of $output.... or when you
assign your $rows to $output, you forgot the bracket (ie. $output[] =
$rows) .... but that is not the case with you posted code

Hendri Kurniawan

Hendri Kurniawan
Apr 20 '07 #2

P: n/a

"Karl Groves" <ka**@NOSPAMkarlcore.comwrote in message
news:Xn*****************************@199.45.49.11. ..
| I'm missing something very obvious, but it is getting late and I've
| stared at it too long.

it is obvious to me only because i have programmed it already...here's a
static method of my db class:

public static function execute($sql, $decode = false, $returnNewId =
false)
{
self::$lastStatement = $sql;
$array = array();
$key = 0;
if (!($records = mysql_query($sql))){ return false; }
$fieldCount = @mysql_num_fields($records);
while ($row = @mysql_fetch_array($records, MYSQL_NUM))
{
for ($i = 0; $i < $fieldCount; $i++)
{
$value = $row[$i];
if ($decode){ $value = self::decode($value); }
$array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
}
$key++;
}
if ($returnNewId)
{
$array = array();
$array[0]['ID'] = mysql_insert_id();
}
@mysql_free_result($records);
return $array;
}

notice, and here's what your code is missing, the use of $key. that way you
create a multi-dimentional array of fields AT $key rather than what you're
getting now. here's example of useage:

$sql = "
SELECT Foo ,
Bar
FROM raspberries
";
$records = db::execute($sql);
foreach ($records as $record)
{
echo '<pre>Foo == ' . $record['FOO'] . '</pre>';
echo '<pre>Bar == ' . $record['BAR'] . '</pre>';
}

hth,

me
Apr 20 '07 #3

P: n/a
| it is obvious to me only because i have programmed it already...here's a
| static method of my db class:

oops...not so obvious. i mis-read how you were setting $output. it looks
like it should work fine.

are you expecting the query to return only a single row (by the nature of
the query), yet are confused as to why you are still getting an array? your
function always returns an array regardless of row count. you could just use
the $numrows and if one, return the single row...all others would still be
arrays of label/value pairs as it should be.

i don't know...it's late. :)
Apr 20 '07 #4

P: n/a
Karl Groves wrote:
But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
Umm... that isn't a multidimensional array. Translation follows:

array(2) { // Start of array with 2 elements
["foo"]= // Element with key "foo" is ...
string(5) "Stuff" // ... a 5 character string "Stuff"
["bar"]= // Element with key "bar" is ...
string(10) "More Stuff" // ... a 10 character string "More Stuff"
} // End of array.

A multidimensional array would look more like this:

array(1) {
[0]=>
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
}

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 20 '07 #5

P: n/a
Toby A Inkster wrote:
Karl Groves wrote:
>But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}

Umm... that isn't a multidimensional array. Translation follows:
He was saying... that he expected multi-dimensional array, but get
something else instead.

Hendri Kurniawan
Apr 20 '07 #6

P: n/a
Hendri Kurniawan wrote:
He was saying... that he expected multi-dimensional array, but get
something else instead.
He may have *meant* that, but it's certainly not what he *said*:

| The intent is that the following code: [...]
| would return an array with the keys: 'foo' and 'bar'.
|
| But what I get is instead a multidimensional array.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 20 '07 #7

P: n/a
Toby A Inkster <us**********@tobyinkster.co.ukwrote in news:nfdmf4-
hc*****@ophelia.g5n.co.uk:
Karl Groves wrote:
>But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}

Umm... that isn't a multidimensional array. Translation follows:

array(2) { // Start of array with 2 elements
["foo"]= // Element with key "foo" is ...
string(5) "Stuff" // ... a 5 character string "Stuff"
["bar"]= // Element with key "bar" is ...
string(10) "More Stuff" // ... a 10 character string "More Stuff"
} // End of array.

A multidimensional array would look more like this:

array(1) {
[0]=>
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
}
Very very sorry that my original post is unclear.

Yes, I am getting a multidimensional array. My var_dump looks exactly
like your second example. (and I wish it looked like the first, lol)

--
Karl Groves

Apr 20 '07 #8

P: n/a
Karl Groves wrote:
I'm missing something very obvious, but it is getting late and I've
stared at it too long.

TIA for responses
I am writing a basic function (listed at the bottom of this post) that
returns data from a query into an array.

The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: 'foo' and 'bar'.

But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I'm wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was <em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.
Hi, Karl,

I'm confused. This should work.

while($rows = mysql_fetch_array($result, $rtype)){

$rows will be a single dimensional array with indexes ['foo'] and ['bar'].

$output[] = $rows;
}
And $output will be an array of the above array with indexes zero to
(number_of_rows_returned - 1).

Are you sure this is the exact code? If so, can you show us where you
call it and dump the results? Maybe the problem is back there.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 20 '07 #9

P: n/a
Jerry Stuckle <js*******@attglobal.netwrote in
news:Vp******************************@comcast.com:
Karl Groves wrote:
>I'm missing something very obvious, but it is getting late and I've
stared at it too long.

TIA for responses
I am writing a basic function (listed at the bottom of this post)
that returns data from a query into an array.

The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: 'foo' and 'bar'.

But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I'm wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was
<em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.

Hi, Karl,

I'm confused. This should work.

while($rows = mysql_fetch_array($result, $rtype)){

$rows will be a single dimensional array with indexes ['foo'] and
['bar'].

$output[] = $rows;
}
And $output will be an array of the above array with indexes zero to
(number_of_rows_returned - 1).

Are you sure this is the exact code? If so, can you show us where you
call it and dump the results? Maybe the problem is back there.
I apologize to everyone for the confusion in my original post.
My var_dump example was wrong.
I'm getting a multidimensional array and want a single dimension.

I recognize that my use of $output[] = $rows; seems to be the reason why
I get the multidimensional array. I guess my real issue is figuring out
how to get ALL returned rows of the single dimensional array. Everything
else I've tried gives me one row.
--
Karl Groves

Apr 20 '07 #10

P: n/a
Karl Groves wrote:
Yes, I am getting a multidimensional array. My var_dump looks exactly
like your second example. (and I wish it looked like the first, lol)
OK then...

function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was <em>$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
elseif ($output = mysql_fetch_array($result, $rtype)){
mysql_free_result($result);
return $output;
}
else {
dbThrowError("Weird-ass error. Query Was <em>$query</em>.");
return FALSE;
}
}
}
--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 20 '07 #11

P: n/a
Karl Groves wrote:
Jerry Stuckle <js*******@attglobal.netwrote in
news:Vp******************************@comcast.com:
>Karl Groves wrote:
>>I'm missing something very obvious, but it is getting late and I've
stared at it too long.

TIA for responses
I am writing a basic function (listed at the bottom of this post)
that returns data from a query into an array.

The intent is that the following code:
$foo = dbSelectData("SELECT foo, bar FROM table", $link);
would return an array with the keys: 'foo' and 'bar'.

But what I get is instead a multidimensional array.
Doing a var_dump on $foo turns out to be
array(2) {
["foo"]=>
string(5) "Stuff"
["bar"]=>
string(10) "More Stuff"
}
I'm wondering where I went wrong in writing the function below.
function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){

$result = mysql_query($query, $connection);
if(!$result){
dbThrowError("Error in function dbSelectData. Query Was
<em>
$query</em>.");
return FALSE;
}
else{
$numrows = mysql_num_rows($result);

if($numrows == 0){
return FALSE;
}
else{
while($rows = mysql_fetch_array($result, $rtype)){
$output[] = $rows;
}
mysql_free_result($result);
return $output;
}
}
}
Thanks in advance.
Hi, Karl,

I'm confused. This should work.

while($rows = mysql_fetch_array($result, $rtype)){

$rows will be a single dimensional array with indexes ['foo'] and
['bar'].

$output[] = $rows;
}
And $output will be an array of the above array with indexes zero to
(number_of_rows_returned - 1).

Are you sure this is the exact code? If so, can you show us where you
call it and dump the results? Maybe the problem is back there.

I apologize to everyone for the confusion in my original post.
My var_dump example was wrong.
I'm getting a multidimensional array and want a single dimension.

I recognize that my use of $output[] = $rows; seems to be the reason why
I get the multidimensional array. I guess my real issue is figuring out
how to get ALL returned rows of the single dimensional array. Everything
else I've tried gives me one row.

Karl,

I guess I don't understand exactly what you want.

You're returning two columns from your SELECT statement - 'foo' and
'bar'. To place them in one variable you need an array.

Now - you're returning multiple instances of that first array (multiple
rows), so you need a multidimensional array to retrieve all the data.

Maybe what you want is

array(2) {
["foo"]=>array(3) {
[0]=>string(9) "foo Stuff"
[1]=>string(14) "more foo stuff"
[2]=>string(20) "still more foo stuff"
}
["bar"]=>array(3) {
[0]string(9) "bar Stuff"
[1]=>string(14) "more bar stuff"
[2]=>string(20) "still more bar stuff"
}
}

Which is still a multidimensional array, but rotated.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 20 '07 #12

P: n/a
Jerry Stuckle <js*******@attglobal.netwrote in
news:Pq******************************@comcast.com:

>
I guess I don't understand exactly what you want.
Why not? I've done such a bangup job of explaining it. ;-)
You're returning two columns from your SELECT statement - 'foo' and
'bar'. To place them in one variable you need an array.

Now - you're returning multiple instances of that first array
(multiple rows), so you need a multidimensional array to retrieve all
the data.
Damn!
That's where my problem was. I was totally backwards in my understanding of
how to deal with the results. Each row is an array, and the function is
returning an array of those arrays.

OK, I only feel a tad bit retarded now.
My issue was expecting the function would return a single dimension array.
Sorry everyone.

--
Karl Groves
Apr 20 '07 #13

P: n/a

"Karl Groves" <ka**@NOSPAMkarlcore.comwrote in message
news:Xn*****************************@199.45.49.11. ..
| Jerry Stuckle <js*******@attglobal.netwrote in
| news:Pq******************************@comcast.com:
|
|
| >
| I guess I don't understand exactly what you want.
|
| Why not? I've done such a bangup job of explaining it. ;-)
|
| You're returning two columns from your SELECT statement - 'foo' and
| 'bar'. To place them in one variable you need an array.
| >
| Now - you're returning multiple instances of that first array
| (multiple rows), so you need a multidimensional array to retrieve all
| the data.
| >
|
| Damn!
| That's where my problem was. I was totally backwards in my understanding
of
| how to deal with the results. Each row is an array, and the function is
| returning an array of those arrays.
|
| OK, I only feel a tad bit retarded now.

no biggie. to clear things up technically...

you have an array of rows, and within ever single row, you have an array of
columns/fields.

cheers.
Apr 20 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.