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, PECL mysql:1.0)
mysql_fetch_row — Get a result row as an enumerated array
Description
array mysql_fetch_row ( resource $result )Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Parameters
- result
The result resource that is being evaluated. This result comes from a call to mysql_query().
Return Values
Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
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.
Examples
Example 1392. Fetching one row with mysql_fetch_row()
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
Notes
Note: This function sets NULL fields to PHP NULL value.
See Also
| mysql_fetch_array() |
| mysql_fetch_assoc() |
| mysql_data_object() |
| mysql_data_seek() |
| mysql_fetch_lengths() |
| mysql_result() |
mysql_fetch_row
17-Nov-2005 09:56
12-Jul-2003 10: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 07: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 01: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 06:40
You could also use this language construct with mysql_fetch_row:
while (list($first, $second) = mysql_fetch_row($resource)) {
[...]
}
13-May-2001 08: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...
