The following function to read all data out of a mysql-resultset, is may be faster than Rafaels solution:
<?
function mysql_fetch_all($result) {
while($row=mysql_fetch_array($result)) {
$return[] = $row;
}
return $return;
}
?>
mysql_fetch_row
(PHP 4, PHP 5)
mysql_fetch_row — Liefert einen Datensatz als indiziertes Array
Beschreibung
$result
)Liefert ein numerisch indizertes Array, das der geholten Zeile entspricht und bewegt den internen Datenatzzeiger vorwärts.
Parameter-Liste
-
Ergebnis -
Das Ergebnis Ressource, das ausgewertet wird. Dieses Ergebnis kommt von einem Aufruf von mysql_query().
Rückgabewerte
Gibt ein numerisches Array von Zeichenketten zurück, das der gelesenen
Zeile entspricht oder FALSE falls keine weiteren Zeilen vorhanden sind.
mysql_fetch_row() liefert einen Datensatz aus dem Anfrageergebnis mit der übergebenen Kennung. Der Datensatz wird als Array geliefert. Jedes Feld wird in einem Array-Offset abgelegt, der Offset beginnt bei 0.
Beispiele
Beispiel #1 Eine Zeile mit mysql_fetch_row() holen
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Konnte Abfrage nicht ausführen: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // der email Wert
?>
Anmerkungen
Hinweis: Diese Funktion setzt NULL-Felder auf den PHP Wert-
NULL.
Siehe auch
- mysql_fetch_array() - Liefert einen Datensatz als assoziatives Array, als numerisches Array oder beides
- mysql_fetch_assoc() - Liefert einen Datensatz als assoziatives Array
- mysql_fetch_object() - Liefert eine Ergebniszeile als Objekt
- mysql_data_seek() - Bewegt den internen Ergebnis-Zeiger
- mysql_fetch_lengths() - Liefert die Länge eines jeden Feldes in einem Ergebnis
- mysql_result() - Liefert Ergebnis
mysql_fetch_row
17-Nov-2005 10:56
13-Jul-2003 12:05
It is probably worth pointing out that the array elements will actually be of type string, OR NULL if the field is null in the database.
Thus, either use a double equal comparison to look for empty or null
Or, use a triple equal comparison to be able to distinguish the two cases
e.g.
if ($field === '') echo "Empty, not NULL\n";
if ($field === NULL) echo "NULL\n";
if ($field == '') echo "Empty or NULL\n";
08-Apr-2003 09:09
to print an array, simply use print_r(array name)
like this:
$myrow = mysql_fetch_row($result);
echo "<pre>";
print_r($myrow);
echo "</pre>";
this will output the array in a readable form, with the index, too. Don't forget the 'pre' tags or the output will be on a single line.
06-Feb-2002 02:10
Maybe worth pointing out that all the fields returned by this (and other?) calls are returned with type string. This had me puzzled for quite some time.
06-Jun-2001 08:40
You could also use this language construct with mysql_fetch_row:
while (list($first, $second) = mysql_fetch_row($resource)) {
[...]
}
13-May-2001 10:57
you can also use mysql_fetch_row() like this if you want to display inumerable results the same way:
#connect to the server and select db
mysql_connect("host","username","pass");
mysql_select_db("dbname");
#query the db
$query = "SELECT * FROM table";
$result = mysql_query($query)
or die(mysql_error());
#display results
while($i = mysql_fetch_row($result)) {
echo $i[0];
echo $i[1];
.....
}
and so forth...