Reputation: 9857
When I make a sql query and echo it out it repeats the first row twice
$sql = 'SELECT one,two,three FROM justAnExampleForSO where one = one';
$info = mysql_query(sql);
if(mysql_num_rows($info )>0){
while ($row = mysql_fetch_array($info )) {
foreach($row as $key => $var)
{
if($key == 'one')
echo $var.",";
else if($key == 'two')
echo $var.",";
else if($key == 'three')
echo $var.";";
}
}
}
so for example say I am attempting to get cols one, two, three When the output is echoed it would echo
one, one, two,three;
I am not sure if this is a duplicate of this question or not because I couldn't full understand his problem. Fetch array function doubling values in each position of array?
Upvotes: 0
Views: 74
Reputation: 522024
Because mysql_fetch_array
returns both numerically indexed values and string indexes, and due to the fun of type casting rules "one"
equals 0
. Use ===
instead of ==
to prevent that, or use mysql_fetch_assoc
to forgo the numerical indexes you're not using anyway.
You should also be doing it much more simply than a foreach..if..else
:
echo $row['one'], ', ', $row['two'], ', ', $row['three'], ';';
Upvotes: 2