473,652 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multidimensiona l Assoc Array with SQL Query Results

Hi,

Is there a way to get a multidimensiona l 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_arr ay($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 5377

"KDawg44" <KD*****@gmail. comwrote in message
news:01******** *************** ***********@a1g 2000hsb.googleg roups.com...
Hi,

Is there a way to get a multidimensiona l 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_arr ay($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::$lastStat ement = $sql;
$array = array();
$key = 0;
if (!($records = mysql_query($sq l))){ return false; }
$fieldCount = @mysql_num_fiel ds($records);
while ($row = @mysql_fetch_ar ray($records, MYSQL_NUM))
{
for ($i = 0; $i < $fieldCount; $i++)
{
$value = $row[$i];
if ($decode){ $value = self::decode($v alue); }
$array[$key][strtoupper(@mys ql_field_name($ records, $i))] = $value;
}
$key++;
}
if ($returnNewId)
{
$array = array();
$array[0]['ID'] = mysql_insert_id ();
}
@mysql_free_res ult($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($sq l);
$myTables['TABLE_B'] = db::execute($sq l);
$myTables['TABLE_C'] = db::execute($sq l);

just assume that $sql reflects the proper query for each 'table'...meani ng,
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....@exampl e.comwrote:
"KDawg44" <KDaw...@gmail. comwrote in message

news:01******** *************** ***********@a1g 2000hsb.googleg roups.com...
Hi,
Is there a way to get a multidimensiona l 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_arr ay($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::$lastStat ement *= $sql;
* * $array * * * * * * * *= array();
* * $key * * * * * * * * *= 0;
* * if (!($records = mysql_query($sq l))){ return false; }
* * $fieldCount * * * * * = @mysql_num_fiel ds($records);
* * while ($row = @mysql_fetch_ar ray($records, MYSQL_NUM))
* * {
* * * for ($i = 0; $i < $fieldCount; $i++)
* * * {
* * * * $value = $row[$i];
* * * * if ($decode){ $value = self::decode($v alue); }
* * * * $array[$key][strtoupper(@mys ql_field_name($ records, $i))]= $value;
* * * }
* * * $key++;
* * }
* * if ($returnNewId)
* * {
* * * $array = array();
* * * $array[0]['ID'] = mysql_insert_id ();
* * }
* * @mysql_free_res ult($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($sq l);
$myTables['TABLE_B'] = db::execute($sq l);
$myTables['TABLE_C'] = db::execute($sq l);

just assume that $sql reflects the proper query for each 'table'...meani ng,
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 multidimensiona l 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_arr ay($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_ass oc($resultSet))
$userArray[] = $row;

mysql_fetch_ass oc is equivalent to mysql_fetch_arr ay 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*******@attgl obal.net
=============== ===

Jul 20 '08 #4
On Jul 19, 8:47*pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
KDawg44 wrote:
Hi,
Is there a way to get a multidimensiona l 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_arr ay($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_ass oc($resultSet))
* * *$userArray[] = $row;

mysql_fetch_ass oc is equivalent to mysql_fetch_arr ay 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...@attgl obal.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...@attg lobal.netwrote:
>KDawg44 wrote:
>>Hi,
Is there a way to get a multidimensiona l associative array with the
entire result set? I would like to get a an array like this:
resultsArra y['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_arr ay($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_ass oc($resultSet))
$userArray[] = $row;

mysql_fetch_as soc is equivalent to mysql_fetch_arr ay 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...@attg lobal.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*******@attgl obal.net
=============== ===

Jul 20 '08 #6

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

Similar topics

1
1650
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 inside loop $q=mysql_query("SHOW FIELDS FROM table" ) or die ("Query failed");
9
6665
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 function (which was translated from Fortran code), among other heinous and numerous declarations, is this bit: static float bbuff; static int bkey; static int buse;
1
2812
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 doesn't give me the results that I wanted. Is there a way to successfully do this? I even tried to bind a multidimensional array (string ) to a DataGrid, but an exception is thrown with the message "Array was not a one-dimensional array". I'm...
4
12061
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 work $results =$temparray doesnt work $results =$temparray doesnt work
3
1340
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 criteiras for the search. The engine needs to be user friendly for general public use so the SQL/query path method is not really an option for me. I have heard or read about multidimensional cube arrays and am not sure if this is a viable method...
11
2346
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 multidimensional array of names that were scanned in order that the file had and be able to access each one by using array for example, to output one of the names. Im very bad at multidimensional array syntax so am asking for help, I could post my code and...
1
1745
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 contains 6months' names. source array contains source of that month. $table_row conatins editions of particular source. looking forward to get help from anyone Thanks Here is the code..
9
4493
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 function: x = new int;
4
2983
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 = array($question1,$question2,$question3); SECOND ARRAY (MULTI-DIMENSIONAL) $baseball = array( array("male","female"), array("right","middle"), array("long","medium","short"));
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5619
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1591
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.