Search code examples
phpphp4

I've some issues with an array $row fetched from MySQL


I've some issues with an array fetched from MySQL

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
mysql_fetch_row($result);

echo $row[0]; // doesn't work
echo $row[1]; // doesn't work

but this work

echo $row["FirstFieldName"] //OK
...

how should I change the following code to make it work?

for ( $i = 0; $i < count( $row ); $i++ )
{
    echo $row[ $i ];
}

thanks


Solution

  • Do below changes. use mysql_fetch_array instead of mysql_fetch_row()

    mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

     $result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
    
    $row=mysql_fetch_array($result);
    
    for ( $i = 0; $i < count( $row ); $i++ )
    {
        echo $row[ $i ];
    }