Page 1 of 1

Pulling out doubles of a record.

Posted: Mon Feb 20, 2006 10:09 am
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

Posted: Mon Feb 20, 2006 10:12 am
by John Cartwright

Code: Select all

echo '<pre>';
while ($line = mysql_fetch_array($result))
{
   print_r($line);
} 
echo '</pre>';
what does this return?

reply

Posted: Mon Feb 20, 2006 10:15 am
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
)


??

Posted: Mon Feb 20, 2006 10:18 am
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.

Posted: Mon Feb 20, 2006 10:19 am
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)

Posted: Mon Feb 20, 2006 10:23 am
by mattyboi
Thanks a lot guys for the quick replies. Works great!

Posted: Mon Feb 20, 2006 10:47 am
by John Cartwright
or

Code: Select all

while ($line = mysql_fetch_assoc($result))

Posted: Mon Feb 20, 2006 11:02 am
by mattyboi
Also, how do I just display a single fields value, instead of the whole record?

Thanks

Posted: Mon Feb 20, 2006 11:08 am
by feyd

Code: Select all

echo $row['fieldname'];