473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Similar topics

1
by: jerrygarciuh | last post by:
Hello, I am using this very slightly modified function found here: http://us2.php.net/mysql_fetch_object to make mySQL rows into objects of type $classname: // This takes db result rows and...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
9
by: Börni | last post by:
Hi, I have an sql query like this: SELECT column FROM table WHERE column1="3" AND column2="1" This query works perfectly if i run it in the command line, to be exactly it return two results. But...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
6
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
21
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function...
2
by: Iain Adams | last post by:
I have a db class that sets up a connection. It then has methods to query the db and fetch results etc that encapsulate the normal mysql functions (i.e. mysql_query($sql)). I seem to get this...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.