469,934 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

mySQL result to real array in function

Hi there!

I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also get the fieldnames returned by mySQL and use those as
keys.

I can't get things to work properly: it should return a
multidimensional array,
like

$result_array[1] = array(
[field1] => field1 value,
[field2] => field2 value,
etc.
)

somehow my result is (with code below)

$result_array[1] = array(
[0] => field1 value,
[field1] => field1 value,
[1] => field2 value,
[field2] => field2 value,
etc.
)

Hope someone can help me on this one.

Frizzle.
+++++ code ++++++

$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[] = $res;

}while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};

Apr 25 '06 #1
16 2209
What i even forgot to mention:

I'd like to have the function called like

CreateArray( $get_result, 'res' );

where 'res' would be the name of the returned array.

Frizzle.

Apr 25 '06 #2
the trick is in ur mysql_fetch_array() function...replace with
mysql_fetch_array( $get_res, 1 )

Should work.

Peace,
Gerard.

Apr 25 '06 #3
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-

$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{
$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};


Apr 25 '06 #4

David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-

$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];

} while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

Apr 25 '06 #5
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-

Apr 25 '06 #6

David Haynes wrote:
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-


I'm sorry, i don't understand what you mean ...
(my lack of english comes to play ... )

Apr 25 '06 #7
Frizzle,

Did the mysql_fetch_array( $get_res, 1 ) change fix the problem?

Apr 25 '06 #8

David Haynes wrote:
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
$get_res = mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[$res['field1']] = $res['field1 value'];
} while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};


David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-


Having read it again and again i get it.
ideally, it would check if there is an 'id' in the returned fields and
use that
as index ...

Frizzle.

Apr 25 '06 #9

Gerard Matthew wrote:
Frizzle,

Did the mysql_fetch_array( $get_res, 1 ) change fix the problem?


Yes thanks gerard, it removed the 'duplicate' indexes on the keys,
only i don't understand why ...

Frizzle.

Apr 25 '06 #10
frizzle wrote:
David Haynes wrote:
frizzle wrote:
David Haynes wrote:
$result[] = causes the result to be pushed onto an indexed array not an
associative array. See inline for fix.

-david-
> $get_res = mysql_query(QUERY);
>
> if( $res = mysql_fetch_array( $get_res ) )
> {
>
> do{
>
$result[$res['field1']] = $res['field1 value'];
> } while( $res = mysql_fetch_array( $get_res ) );
>
> };
>
> foreach( $result as $key => $value ){
>
> print_r($value);
>
> };
David,

i don't alwas know all fieldnames beforehand.
So SELECT * should also put all fieldnames as
keys in the array.

Frizzle.

My assumption was that one column in the table being queried formed the
keys for the associative array. If so, then my method works. If not,
what were you planning on using as the key to the array?

-david-


Having read it again and again i get it.
ideally, it would check if there is an 'id' in the returned fields and
use that
as index ...

Frizzle.


Can you provide a sample of the table you are querying? It doesn't have
to contain real data but should include the column names and an example
of the array result you would like. It's easier to provide you with help
if the example is close to the problem.

Something like:
FOO
login Name Address1
fred Fred Flinstone 123 Anywhere
barney Barney Rubble 125 Anywhere

array:
result['fred'] = ...
result['barney'] = ...

-david-

Apr 25 '06 #11
Gerard Matthew wrote:
Frizzle,

Did the mysql_fetch_array( $get_res, 1 ) change fix the problem?


This would probably be clearer as:
mysql_fetch_array($get_res, MYSQL_ASSOC);

-david-
(I hate magic numbers in code ;-) )

Apr 25 '06 #12
Frizzle,

mysql_fetch_array() by default returns both result types. MYSQL_ASSOC
and MYSQL_NUM

Therefore mysql_fetch_array($result, MYSQL_BOTH) is the same as
mysql_fetch_array($result) based on the return values.

For numeric indicies you use MYSQL_NUM...
mysql_fetch_array($result,MYSQL_NUM);
and for associative indicies you would use MYSQL_ASSOC...
mysql_fetch_array($result,MYSQL_ASSOC).

Hope this helps out in understanding.

Rgds,
Gerard.

Apr 25 '06 #13

Gerard Matthew wrote:
Frizzle,

mysql_fetch_array() by default returns both result types. MYSQL_ASSOC
and MYSQL_NUM

Therefore mysql_fetch_array($result, MYSQL_BOTH) is the same as
mysql_fetch_array($result) based on the return values.

For numeric indicies you use MYSQL_NUM...
mysql_fetch_array($result,MYSQL_NUM);
and for associative indicies you would use MYSQL_ASSOC...
mysql_fetch_array($result,MYSQL_ASSOC).

Hope this helps out in understanding.

Rgds,
Gerard.


Yes it does. Thanks!

@David:

table: FOO
id login Name Address1
1 fred Fred Flinstone 123 Anywhere
2 barney Barney Rubble 125 Anywhere

array:
result[1][login] = fred
result[2][name] = Barney Rubble

Something like that. But only if 'id' is a returned field,
else use default array indices.
(and how could i define the array's name as another
function var ? )

Anyways, both you guys helped me a lot already!

Frizzle.

Apr 25 '06 #14
frizzle wrote:
Gerard Matthew wrote:
Frizzle,

mysql_fetch_array() by default returns both result types. MYSQL_ASSOC
and MYSQL_NUM

Therefore mysql_fetch_array($result, MYSQL_BOTH) is the same as
mysql_fetch_array($result) based on the return values.

For numeric indicies you use MYSQL_NUM...
mysql_fetch_array($result,MYSQL_NUM);
and for associative indicies you would use MYSQL_ASSOC...
mysql_fetch_array($result,MYSQL_ASSOC).

Hope this helps out in understanding.

Rgds,
Gerard.
Yes it does. Thanks!

@David:

table: FOO
id login Name Address1
1 fred Fred Flinstone 123 Anywhere
2 barney Barney Rubble 125 Anywhere

array:
result[1][login] = fred
result[2][name] = Barney Rubble

Something like that. But only if 'id' is a returned field,


I'm having trouble with what you mean by this. I am going to assume
that you mean 'when id is not null'.
else use default array indices.
(and how could i define the array's name as another
function var ? )

Anyways, both you guys helped me a lot already!

Frizzle.


I take it the database design is not something you can change. sigh!

OK, how about this?

$default = 'login'; // The default column to use if 'id' is not set
while( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
if( $row['id'] != '' ) { // id is not null
$return[][$row['id']] = $row[$row['id']];
} else { // id is null
$return[]['default'] = $row[$default];
}
}

-david-

Apr 25 '06 #15
David Haynes wrote:
Gerard Matthew wrote:

Frizzle,

Did the mysql_fetch_array( $get_res, 1 ) change fix the problem?


This would probably be clearer as:
mysql_fetch_array($get_res, MYSQL_ASSOC);

-david-
(I hate magic numbers in code ;-) )

Or use mysql_fetch_assoc instead of mysql_fetch_array.
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Apr 25 '06 #16
Chuck Anderson wrote:
David Haynes wrote:
Gerard Matthew wrote:

Frizzle,

Did the mysql_fetch_array( $get_res, 1 ) change fix the problem?


This would probably be clearer as:
mysql_fetch_array($get_res, MYSQL_ASSOC);

-david-
(I hate magic numbers in code ;-) )

Or use mysql_fetch_assoc instead of mysql_fetch_array.
--
*****************************
Chuck Anderson · Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************


Thanks guys!
I have it working now. (AFAIK perfectly!)

My function is below.
Thanks again guys!

++++ code ++++

function doQuery( $query, $res_key = NULL ){

$get_res = @mysql_query( $query ) or die(mysql_error());

if( $res = mysql_fetch_assoc( $get_res ) )
{
do{
if( $res_key !== NULL ){
$result[ $res[ $res_key ] ] = $res;
}else{
$result[] = $res;
};
}while( $res = mysql_fetch_assoc( $get_res ) );
}
else
{
$result = false;
};
return $result;
};

++++ / code ++++

you could call it as following:
$my_new_array = doQuery( $query, 'id' ); // will return id (from the
query) as array index
$some_other_name = doQuery( $query ); // will return array with default
index.

Thanks a bunch guys.
(It looks so simple now ...)

Frizzle.

Apr 26 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by jerrygarciuh | last post: by
reply views Thread by Phil Powell | last post: by
9 posts views Thread by Börni | last post: by
3 posts views Thread by auron | last post: by
6 posts views Thread by Duderino82 | last post: by
21 posts views Thread by bruno_guedesav | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.