Tuesday, June 22, 2010

Hello Friends!!
Today lets talk about mysql_fetch_array()
mysql_fetch_array() is a function used in MySQL.While developing applications on PHP, it neccessary to have a good knowledge about the structured query language too.This mysql_fetch_array() is a function of SQl,basically a command. It returns an array that corresponds to the fetched row and moves the internal data pointer ahead. Returns an array that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works). Let me explain you with an example.

Example. mysql_fetch_array() with MYSQL_NUM

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$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]);
}

mysql_free_result($result);
?>
Example. mysql_fetch_array() with MYSQL_ASSOC

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>

Example. mysql_fetch_array() with MYSQL_BOTH

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}

mysql_free_result($result);
?>

There is one more function that is mysql_fetch_row() . 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.

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 a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.

I think this much knowledge is enough. I'll show an application on this tomorrow. Guys Keep commenting on my blog. You can get a lot of knowledge on this blog. And knowledge is very important in life. See you tomorrow.

No comments:

Post a Comment