Pulling out doubles of a record.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Pulling out doubles of a record.

Post by mattyboi »

Hey guys,

For some reason I am pulling out two of every field of one record in my table.

Heres my PHP.

Code: Select all

<?php
$imgid = 1;
$imgtype = 'Walkways';
$imgcat = 'Stone';

$link = mysql_connect("mysql", "glenizett", "1phoenix");
mysql_select_db("phoenixmason");

$query = 'SELECT * FROM `images` WHERE `ImgID` = ' . $imgid . ' and `ImgType` = "' . $imgtype . '" and `ImgCat` = "' . $imgcat . '"';

$result = mysql_query($query);

while ($line = mysql_fetch_array($result))
{
	foreach ($line as $value)
	{
		print "$value\n";
	}
}

mysql_close($link);
?>
I think it has something to do with my loop but I can't figure it out. Here is my output.

1 1 Walkways Walkways Stone Stone VA VA Loudoun County Loudoun County Ashburn Ashburn Concrete Concrete Pine Hall Pine Hall Full Range Full Range Running Bond Running Bond

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

echo '<pre>';
while ($line = mysql_fetch_array($result))
{
   print_r($line);
} 
echo '</pre>';
what does this return?
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

reply

Post by mattyboi »

That returns:

Array
(
[0] => 1
[ImgID] => 1
[1] => Walkways
[ImgType] => Walkways
[2] => Stone
[ImgCat] => Stone
[3] => VA
[State] => VA
[4] => Loudoun County
[County] => Loudoun County
[5] => Ashburn
[City] => Ashburn
[6] => Concrete
[Base] => Concrete
[7] => Pine Hall
[Pavor] => Pine Hall
[8] => Full Range
[Color] => Full Range
[9] => Running Bond
[Pattern] => Running Bond
)


??
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

mysql_fetch_assoc()

It looks like you are receiving the result array indexed by numeral and key. Fetching the associative values only should ensure your array only holds one copy of the values needed. Your array (try var_dump() ) may look like:

array {
0 => 'value1',
'first' => 'value1',
1 => 'value2',
'second' => 'value2',
}

Having both numeric and full text keys ( or not ). Its how mysql returns results by default.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

By default mysql_fetch_array() returns both an associative array and a numerically indexed array

change

Code: Select all

while ($line = mysql_fetch_array($result))
to

Code: Select all

while ($line = mysql_fetch_array($result),MYSQL_ASSOC)
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Post by mattyboi »

Thanks a lot guys for the quick replies. Works great!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

or

Code: Select all

while ($line = mysql_fetch_assoc($result))
mattyboi
Forum Commoner
Posts: 34
Joined: Mon Feb 06, 2006 9:42 pm

Post by mattyboi »

Also, how do I just display a single fields value, instead of the whole record?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo $row['fieldname'];
Post Reply