Reputation: 1577
Hiii, I am getting multiple value form database when I am using foreach, plz help
function display($host,$user,$pass,$database)
{
$db = mysql_connect($host, $user, $pass);
mysql_select_db ($database);
$query = "SELECT * FROM `sysdes_moduleinfo`";
$result = mysql_query($query) OR die(mysql_error());
$i=0;
while($row = mysql_fetch_array($result))
{
/*$max = count($row);
while($i<6) {
echo $row[$i]." ";
$i++;
}*/
foreach ($row as $value)
{
//echo $value . " ";
echo htmlspecialchars($value);
}
echo "<br/>";
}
This what I get with this code.
.
This what I have in the database.
Upvotes: 2
Views: 104
Reputation: 4656
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach ($row as $value)
{
//echo $value . " ";
echo htmlspecialchars($value);
}
echo "<br/>";
}
mysql_fetch_array
returns both a numeric and associative array.
Upvotes: 1
Reputation: 3443
The default result type for mysql_fetch_array is to return data in both a number and associative array. This is why data is duplicated. Try using mysql_fetch_row instead.
However it should be noted that both mysql_fetch_array & mysql_fetch_row are both deprecated.
Upvotes: 5