How does the Mysql result set works in php

Written By Naveen on Wednesday, December 7, 2011 | 8:58 AM

Normally if you want to connect to the mysql server the php function is mysql_connect('hostname','username','password')   and to connect the database mysql_select_db('database_name').


And then we will write a query and will execute like this
$resultset  =   mysql_query("SELECT * FROM employees");
Now from the $resultset we have to fetch the data.
In php we have 4 mysql functions to fetch the data from result set
  • mysql_fetch_row($resultset)
  • mysql_fetch_array($resultset)
  • mysql_fetch_assoc($resultset)
  • mysql_fetch_object($resultset)
see the code example below.

mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");

$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



$result = mysql_query("select id,email from mytable");
while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
}
mysql_free_result($result);


while ($row = mysql_fetch_assoc($result)) {
   echo $row["id"];
   echo $row["email"];
}

while ($row = mysql_fetch_array($result)) {
    echo ( $row[0], $row[1]);  
    echo ( $row['id'], $row['email']);  
}



2 comments:

Vivek said...
This comment has been removed by the author.
Anonymous said...

There are no updates from ur site y?

Post a Comment