Abhishek Patidar
Abhishek Patidar

Reputation: 1577

php showing multiple echo when retrieving from the database

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. enter image description here.

This what I have in the database. enter image description here

Upvotes: 2

Views: 104

Answers (2)

som
som

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

AeroX
AeroX

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

Related Questions