473,326 Members | 2,655 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,326 software developers and data experts.

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 2713
Pelin,

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

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*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFED2EI3jcQ2mg3Pc8RAn9SAJ4kf0YGvBvxCViO2Cpsnj CBB6fYyACePyTF
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
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
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...
6
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...
34
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
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...
35
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
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...
41
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...
3
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 =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.