473,395 Members | 1,639 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,395 software developers and data experts.

Multidimensional Assoc Array with SQL Query Results

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['TableKey']['columnsInTable']

How can I accomplish this? Can I do something like this?

var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = $row;
}

This is not correct but I am hoping it is at least a starting point.
Thanks.

Kevin
Jul 19 '08 #1
5 5367

"KDawg44" <KD*****@gmail.comwrote in message
news:01**********************************@a1g2000h sb.googlegroups.com...
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['TableKey']['columnsInTable']

How can I accomplish this? Can I do something like this?

var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = $row;
}

This is not correct but I am hoping it is at least a starting point.
it's not bad. what i would say in looking at it is that you only need to
initialize $userArray = array()...the inner, empty array doesn't allocate
space...it's not needed. that said, here's what i use in a mysql
implementation of an abstract 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;
}

forget the 'decode' stuff since i didn't post the function. anyway, that
would be the basis for returning a single resultset. to multi-dimension it,
just make a key for the table:

$myTables['TABLE_A'] = db::execute($sql);
$myTables['TABLE_B'] = db::execute($sql);
$myTables['TABLE_C'] = db::execute($sql);

just assume that $sql reflects the proper query for each 'table'...meaning,
i'm not showing in the example above that $sql represents a new query for
each 'execute'.

hope that helps...fwiw, you're already on the right track.

cheers.
Jul 19 '08 #2
On Jul 19, 7:42*pm, "Dale" <the....@example.comwrote:
"KDawg44" <KDaw...@gmail.comwrote in message

news:01**********************************@a1g2000h sb.googlegroups.com...
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['TableKey']['columnsInTable']
How can I accomplish this? *Can I do something like this?
* * * *var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = *$row;
}
This is not correct but I am hoping it is at least a starting point.

it's not bad. what i would say in looking at it is that you only need to
initialize $userArray = array()...the inner, empty array doesn't allocate
space...it's not needed. that said, here's what i use in a mysql
implementation of an abstract 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;
* }

forget the 'decode' stuff since i didn't post the function. anyway, that
would be the basis for returning a single resultset. to multi-dimension it,
just make a key for the table:

$myTables['TABLE_A'] = db::execute($sql);
$myTables['TABLE_B'] = db::execute($sql);
$myTables['TABLE_C'] = db::execute($sql);

just assume that $sql reflects the proper query for each 'table'...meaning,
i'm not showing in the example above that $sql represents a new query for
each 'execute'.

hope that helps...fwiw, you're already on the right track.

cheers.
Thank you for your help. I'll give that a whirl.

Kevin
Jul 20 '08 #3
KDawg44 wrote:
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['TableKey']['columnsInTable']

How can I accomplish this? Can I do something like this?

var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = $row;
}

This is not correct but I am hoping it is at least a starting point.
Thanks.

Kevin
You're close, Kevin.

$userArray = array();
while ($row = mysql_fetch_assoc($resultSet))
$userArray[] = $row;

mysql_fetch_assoc is equivalent to mysql_fetch_array with MYSQL_ASSOC.

The results will now be in $userArray[0] .. [n].

$userarray[$x]['column_name'] will contain the contents of 'column_name'
for row $x. You can iterate through it with a for loop, foreach(), etc.

The only thing to remember is this can take a lot of memory - especially
if you're returning a large number of rows with a lot of data in each row.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 20 '08 #4
On Jul 19, 8:47*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
KDawg44 wrote:
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['TableKey']['columnsInTable']
How can I accomplish this? *Can I do something like this?
* * * * var $userArray = array(array());
* *for ($i=0; $i<$numResults; $i++) {
* * * * * *$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
* * * * * *$userArray[$row['Key']] = *$row;
* *}
This is not correct but I am hoping it is at least a starting point.
Thanks.
Kevin

You're close, Kevin.

* *$userArray = array();
* *while ($row = mysql_fetch_assoc($resultSet))
* * *$userArray[] = $row;

mysql_fetch_assoc is equivalent to mysql_fetch_array with MYSQL_ASSOC.

The results will now be in $userArray[0] .. [n].

$userarray[$x]['column_name'] will contain the contents of 'column_name'
for row $x. *You can iterate through it with a for loop, foreach(), etc..

The only thing to remember is this can take a lot of memory - especially
if you're returning a large number of rows with a lot of data in each row..

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Thanks. I was able to get this working the way I wanted. Is there a
better way to get to the data? I am making this into XML to return to
an AJAX call where I am passing in the data in an assoc array and then
processing like this:

function formatDataToXML($data) {
$XMLString = "<?xml version='1.0' encoding='utf-8'?><DataRoot>";
foreach ($data as $key =$row) {
$XMLString .= "<" . $key . ">";
foreach ($row as $col =$val) {
$XMLString .= "<" . $col . ">" . $val . "</" . $col . ">";
}
$XMLString .= "</" . $key . ">";
}
$XMLString .= "</DataRoot>";
return $XMLString;
}

Thanks.
Jul 20 '08 #5
KDawg44 wrote:
On Jul 19, 8:47 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>KDawg44 wrote:
>>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['TableKey']['columnsInTable']
How can I accomplish this? Can I do something like this?
var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_array($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = $row;
}
This is not correct but I am hoping it is at least a starting point.
Thanks.
Kevin
You're close, Kevin.

$userArray = array();
while ($row = mysql_fetch_assoc($resultSet))
$userArray[] = $row;

mysql_fetch_assoc is equivalent to mysql_fetch_array with MYSQL_ASSOC.

The results will now be in $userArray[0] .. [n].

$userarray[$x]['column_name'] will contain the contents of 'column_name'
for row $x. You can iterate through it with a for loop, foreach(), etc.

The only thing to remember is this can take a lot of memory - especially
if you're returning a large number of rows with a lot of data in each row.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Thanks. I was able to get this working the way I wanted. Is there a
better way to get to the data? I am making this into XML to return to
an AJAX call where I am passing in the data in an assoc array and then
processing like this:

function formatDataToXML($data) {
$XMLString = "<?xml version='1.0' encoding='utf-8'?><DataRoot>";
foreach ($data as $key =$row) {
$XMLString .= "<" . $key . ">";
foreach ($row as $col =$val) {
$XMLString .= "<" . $col . ">" . $val . "</" . $col . ">";
}
$XMLString .= "</" . $key . ">";
}
$XMLString .= "</DataRoot>";
return $XMLString;
}

Thanks.
If that's all you're doing, you don't need to get everything into one
large array. You can use SimpleXML to build your XML as you retrieve
each row, and when you're done, write the whole works to a file. Much
easier.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 20 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pauld | last post by:
from a MySQL DB i want to get a multidimensional array that i can loop through either key =field name value = array of ENUM options or array =field name, array= ENUM options and increment x...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: epigram | last post by:
Well, conceptually this is what I want to do. I was hoping to use an ArrayList to build a (dynamic) array of string arrays, and then bind the ArrayList object to a DataGrid. I can do that, but it...
4
by: pauld | last post by:
$sql= sql query $i=0; while ($a2=mysql_fetch_array($a1)){array_push($temparray,$a2,$a2,$a2,$a2); { I want this array to be the value of an asssoc. array $results $results =$temparray doesnt...
3
by: BobbyS | last post by:
I am trying to develop a multidimensional array for use of searching a very large database. I understand the concept of one and two dimensional arrays but this project would include up to 12 or 13...
11
by: Bigshot | last post by:
Im trying to scan a file using fscanf and want to put the results in a multidimensional array (since C has no strings I need it to store names). Basically I want to be able to have a...
1
by: shailajaAdiga | last post by:
Hi All, there are 4 different categories which each month will bw updated. In each category(source),there are many editions. I have to display 6months updates. its like one is month array which...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
4
by: jgendr2 | last post by:
So here is my problem I do not know if there is another way to solve this without using arrays....but I am assuming that I DO need to use arrays....Anyways FIRST ARRAY (SINGLE): $results =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...

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.