473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem iterating thru associative array created by using SQL query.

Hi,

I am using PHP 5.1 and MySQL. I have a result set obtained by executing
PDO:: query. I want to create an XML file by using this result set. The
XML file will contain column names as XML node name and column values
as node values.

$orders = $db->query($sql);

if (!empty($orders )) {

foreach ($orders as $row) {

foreach($row as $key=>$value) {
print $key . " => " . $value . "<br>";
// XML Elements will be created here...
}
}
}

The result of this loop as follows:

orderID => 1
0 => 1
packageSKU => SKU1
1 => SKU1
fullName => John Doe
2 => John Doe
address =>address1
3 =>address1
city => city1
4 => city1
itemTypeLabel => mailer
5 => mailer

When I am iterating through foreach loop, I am getting associative
array names and indices together. For example, after getting orderID as
$key in the first run, I am getting 0 as $key in the second run, etc.
which I don't want to. I just want to use the associative array names.

I appreciate if you can let me know if you have any idea what might be
the reason.

Thanks in advance.

Sincerely,

Pelin Bali.

Mar 8 '06 #1
3 2743
Pelin,

Try changing the line
$orders = $db->query($sql);
to :
$orders = $db->query($sql, PDO::FETCH_ASSO C);

I don't know why that option is not shown on the php.net docs for PDO,
but it does infact work for me (php 5.1.2)

More info can be found here:
http://wiki.cc/php/PDO_Basics#Query_a_table

Hope that helps,
Carl.

Mar 8 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pbali wrote:
I appreciate if you can let me know if you have any idea what might be
the reason.


RTFM:

http://php.net/mysql_fetch_array

And check the documentation of your DB abstraction layer, to see if it's
possible to change between MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_********* *************** *@hotmail.com
Jabber:iv****** ***@jabber.org ; iv*********@kde talk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFED2EI3jc Q2mg3Pc8RAn9SAJ 4kf0YGvBvxCViO2 CpsnjCBB6fYyACe PyTF
A5hxQWZWt/auC/F/fpYqKZo=
=SF+8
-----END PGP SIGNATURE-----
Mar 8 '06 #3
Thanks Carl,

That worked. *:)

-Pelin.

Mar 9 '06 #4

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

Similar topics

27
11156
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
2
1544
by: Zenobia | last post by:
I have a problem. I need to look up several values stored in arrays. Each value is stored as a pair. The first (number) represents the probability of this item occurring. For instance, in the example below there are 11 items. conFinal = {2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k',16:'s',18:'hl',19:'tl',20:'sh'} Letter 'n' (2nd item) has a probability of 2 chances in 20 (1 in 10] but 'tl' has only a 1 in 20 chance. The chance is
6
2008
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference between London and the previous element is 25 (40-15), London has a 25% chance of being selected. When I call the function getAssocItem to do this I need to send in 2 arguments. Is there a quick way to get the maximum key value in the associative
34
4185
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
14
6694
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where this.menuItems is an associative array. The problem occurred when
35
6623
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
2403
by: geoffcastro | last post by:
I am using a SQL query to pull data that I need for a report. I can get it to print out just fine on screen, but I can't figure out how to get the data into an associative array. I have been playing with this for the past few days and cannot get over the hump on this one. Can someone please help? The reason i need to get this data into an associative array is because the data represents a report I need to export into Excel via the...
41
4928
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
3
5156
by: Robert Mark Bram | last post by:
Hi all, I have some code to iterate through nested associative arrays. Can anyone please tell me what I am doing wrong? :) var dealers = new Array(); var dealer1 = new Array(); dealer1 = "Jefferson Ford Pty Ltd"; dealer1 = "215-217 Normanby Rd South Melbourne VIC 3205";
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
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...
1
8522
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
8622
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...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
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.