Monday, April 16, 2007

Fetch data from the tables

There is Mainly Four ways to fetch data from the tables

  • mysql_fetch_array($query_result)

this function will fetch data from the query result by array you can access this array by the key which is table's field name and also by the id start with [0] and upto no. of filed in the table.

eg:

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

// printf("ID: %s Name: %s", $row['id'], $row['name']);

}

  • mysql_fetch_object($query_result)

this function will fetch data from the query result by object you can access this object memebers bye the -> arrow operator and its key value .

$result = mysql_query("select * from mytable");

while ($row = mysql_fetch_object($result))

{

echo $row->id; echo $row->name;

}

  • mysql_fetch_row($query_result)

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 * from mytable");
while ($row = mysql_fetch_row($result))
{
echo $row[0]; echo $row[1];
}

  • mysql_fetch_assoc($query_result)

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.

while ($row = mysql_fetch_assoc($result))

{

echo $row["id"]; echo $row["name"]; echo $row["user"];

}

No comments: