I’m using PHP 5 with mySQL:
$line = mysql_fetch_array($result, MYSQL_ASSOC);
When I do this, I can see the results of the query printed:
foreach ($line as $data)
{
echo ($data . ", ");
}
But if I do this instead:
echo ($line[0] . " " . $line[1] . " " . $line[2]);
I don’t see anything. Also I can’t assign a value from $line:
$values[] = $line[0]; // fails – doesn’t assign anything
Why? And what should I be doing instead?
You’ve nominated to retrieve records as associative entries only (
MYSQL_ASSOC). This means your$linearray will not contain numeric indices.If you only want numeric arrays, use
mysql_fetch_row().Associative only,
mysql_fetch_assoc()You can also retrieve a mixed numeric and associative array using
mysql_fetch_array($result, MYSQL_BOTH)however this will give you duplicate entries in yourforeachloop.