By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Strange mysql_fetch_array() behavior

P: n/a
Hi,

I have the following code:

$resultSet = mysql_query($sqlString);
$numResults = mysql_num_rows($resultSet);
echo "Num Results = " . $numResults . "<br/>";
for ($i = 0; $i < $numResults; $i++) {
$row = mysql_fetch_array($resultSet);
echo "i = " . $i . "<br/>";
foreach ($row as $key=>$r) {
echo $key . " = " . $r . "<br/>";
}
}

When I run the query at the MySQL Command prompt, I get exactly one
record, which is correct. However, when I echo the key value pairs,
it is printing the values twice, once with a numeric key (index) and
then again with the proper key name. For example, here is the first
bit of the output:

i = 0
0 = 2
ApptID = 2
1 = 1
CustID = 1
2 = 1
DogID = 1
3 = 2
4 = 12:00:00
ApptTime = 12:00:00
5 = 03:00:00
PickupTime = 03:00:00
6 = 1
7 = Mac
Name = Mac
8 = 1
9 = Kevin and Erika
FName = Kevin and Erika
10 = Holleran
LName = Holleran

I just want the key=>pairs! How do I get rid of the index thing?!

Thanks for any help.

Kevin
Feb 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
KDawg44 wrote:
Hi,

I have the following code:

$resultSet = mysql_query($sqlString);
$numResults = mysql_num_rows($resultSet);
echo "Num Results = " . $numResults . "<br/>";
for ($i = 0; $i < $numResults; $i++) {
$row = mysql_fetch_array($resultSet);
echo "i = " . $i . "<br/>";
foreach ($row as $key=>$r) {
echo $key . " = " . $r . "<br/>";
}
}

When I run the query at the MySQL Command prompt, I get exactly one
record, which is correct. However, when I echo the key value pairs,
it is printing the values twice, once with a numeric key (index) and
then again with the proper key name. For example, here is the first
bit of the output:

i = 0
0 = 2
ApptID = 2
1 = 1
CustID = 1
2 = 1
DogID = 1
3 = 2
4 = 12:00:00
ApptTime = 12:00:00
5 = 03:00:00
PickupTime = 03:00:00
6 = 1
7 = Mac
Name = Mac
8 = 1
9 = Kevin and Erika
FName = Kevin and Erika
10 = Holleran
LName = Holleran

I just want the key=>pairs! How do I get rid of the index thing?!

Thanks for any help.

Kevin
http://us3.php.net/manual/en/functio...etch-array.php
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 10 '08 #2

P: n/a
On Feb 9, 10:21 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
KDawg44 wrote:
Hi,
I have the following code:
$resultSet = mysql_query($sqlString);
$numResults = mysql_num_rows($resultSet);
echo "Num Results = " . $numResults . "<br/>";
for ($i = 0; $i < $numResults; $i++) {
$row = mysql_fetch_array($resultSet);
echo "i = " . $i . "<br/>";
foreach ($row as $key=>$r) {
echo $key . " = " . $r . "<br/>";
}
}
When I run the query at the MySQL Command prompt, I get exactly one
record, which is correct. However, when I echo the key value pairs,
it is printing the values twice, once with a numeric key (index) and
then again with the proper key name. For example, here is the first
bit of the output:
i = 0
0 = 2
ApptID = 2
1 = 1
CustID = 1
2 = 1
DogID = 1
3 = 2
4 = 12:00:00
ApptTime = 12:00:00
5 = 03:00:00
PickupTime = 03:00:00
6 = 1
7 = Mac
Name = Mac
8 = 1
9 = Kevin and Erika
FName = Kevin and Erika
10 = Holleran
LName = Holleran
I just want the key=>pairs! How do I get rid of the index thing?!
Thanks for any help.
Kevin

http://us3.php.net/manual/en/functio...etch-array.php

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
By strange behavior i meant normal behavior.... thanks! I use a text
as a reference (little older, maybe that's why) and it did not say
anything about this.

Thanks again Jerry! You are the king.
Feb 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.