Friday, May 17, 2013

How to retrieve the data from MySQL result set ?


using the methods given below 
1. mysql_fetch_row. 
2. mysql_fetch_array 
3. mysql_fetch_object 
4. mysql_fetch_assoc



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.
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name (by using 'field' in this example).

mysql_fetch_array() with MYSQL_NUM
<?php
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);?> 

1 comment: