473,511 Members | 16,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query returning array

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
13 2534
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

"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
| 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
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
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
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
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
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
10269
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
3752
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
3
2672
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
19560
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
17
3219
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
4099
by: Rowan | last post by:
For some reason when I do a fetchall on a PDO query the array returned has duplicated keys. see below. SELECT name_first, name_last, personal_street, personal_phone_home, personal_phone_cell...
5
5372
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do...
2
5583
by: paulmitchell507 | last post by:
I think I am attempting a simple procedure but I just can't figure out the correct syntax. My asp (classic) page runs a SELECT query to obtain dates and ID's from 2 tables uSQL = "SELECT...
5
2666
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.