Tizag.com Webmaster Tutorials - A collection of webmaster tutorials from HTML to PHP.

Tuesday, September 9, 2008

PHP Associated MySQL Functions

mysql_fetch_object: The mysql_fetch_object() function returns a row from a recordset as an object. It 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. This function gets a row from the mysql_query() function and returns an object on success, or FALSE on failure or when 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).

$result = mysql_query($sql,$con);
while ($row = mysql_fetch_object($result)) {
echo $row->FirstName . "
";
}


***********************************************************************

mysql_fetch_row: Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead. 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.

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value


***********************************************************************

mysql_fetch_array: Fetch a result row as an associative array, a numeric array, or both. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows. Optional. Specifies what kind of array to return.

Possible values:
* MYSQL_ASSOC - Associative array
* MYSQL_NUM - Numeric array
* MYSQL_BOTH - Default. Both associative and numeric array


$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
------------------------------ or ----------------------------------------
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysql_free_result($result);


***********************************************************************

mysql_fetch_assoc:Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

***********************************************************************

No comments: