A Closer Look: Five MySQL functions that handle query results
Assigning variable names to a MySQL result set
After executing a query to select data from a MySQL table there are several functions you can use to perform operations on the retrieved data.
mysql_result() - fetches the contents of the specified cell
mysql_data_seek() - moves to the specified row of a result set
mysql_fetch_array() - fetches the next row in the result set as an array
mysql_fetch_object() - fetches the next row in the result set as an object
mysql_fetch_row() - fetches the next row in the result set as an enumerated array
If you expect only one row for your result, you could use just the mysql_result function:
PHP Code:
<?php
$result = mysql_query("SELECT ID, Name, Desc FROM Categories where id=1");
$cid=mysql_result($result,0,"id");
$cname=mysql_result($result,0,"cname");
$desc=mysql_result($result,0,"desc");
echo "<i>$cid $cname: $desc </i><br>\n";
?>mysql_data_seek() Move internal result pointer
Returns TRUE on success or FALSE on failure.
mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to mysql_fetch_row() would return that row.
Row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows - 1. - PHP Manual:mysql_data_seek
Note: mysql_data_seek($result, 0) returns an error on an empty table so you should check how many rows of data are retrieved from the database
PHP Code:
<?php
$result4 = mysql_query("SELECT ID, Desc FROM Categories");
$rows = mysql_num_rows($result4);
//echo $rows;
if($rows>0){
mysql_data_seek($result4,3);
$row = mysql_fetch_array($result4);
echo "<u>".$row["ID"] ."</u> ". $row["Desc"] ."<br>\n";
}
?>mysql_fetch_array() Fetch a result row as an associative array, a numeric array, or both.
Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys. - PHP Manual:mysql_fetch_array
PHP Code:
<?php
$result = mysql_query("SELECT ID, Name FROM Categories");
while ($row=mysql_fetch_array($result)){
echo $row["cid"] $row["cname"] $row["desc"] ."<br>\n";
}
?>mysql_fetch_object() Fetch a result row as an object
Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.
mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names). - PHP Manual:mysql_fetch_object
Note:The column names are 'case sensitive.'
PHP Code:
<?php
$result5 = mysql_query("SELECT ID, Name FROM Categories where id=5");
while ($row=mysql_fetch_object($result5)){
echo $row->ID." : ".$row->Name;
}
?>mysql_fetch_row() Get a result row as an enumerated array
Returns an array 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.
Subsequent call to mysql_fetch_row() would return the next row in the result set, or FALSE if there are no more rows.
- PHP Manual:mysql_fetch_row
PHP Code:
<?php
$cats = mysql_query("SELECT ID, Name FROM Categories");
while ($cat = mysql_fetch_row ($cats)){
print "$cat[0]/ $cat[1]<br>\n";
}
?>