Pages

Subscribe:

Saturday, November 19, 2011

Difference between mysql_fetch_assoc, mysql_fetch_row,mysql_fetch_object and mysql_fetch_array


Definition :

mysql_fetch_array():-

Fetch a result row as an associative array, a numeric array, or both by default it fetches both.
Returns an array of strings that corresponds to the fetched row, or FALSE  if there are no more rows. The type of returned array depends on how result_typeis 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).

mysql_fetch_row() :-

Get a result row as an numeric array
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.

mysql_fetch_object :-

Fetch a result row as an object
Returns an object with string properties that correspond to the fetched row, orFALSE;if there are no more rows.

mysql_fetch_assoc() :-

Fetch a result row as an associative array
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to callingmysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

Example:

<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_array ($result)) {
    echo $row[0];
    echo $row[1];
    echo $row[2];
}

while ($row = mysql_fetch_array ($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

while ($row = mysql_fetch_array ($result)) {
    echo $row[0];
    echo $row["fullname"];
    echo $row[2];
}

while ($row = mysql_fetch_row($result)) {
    echo $row[0];
    echo $row[1];
    echo $row[2];
}

while ($row = mysql_fetch_object($result)) {
    echo $row->userid;
    echo $row->fullname;
    echo $row->userstatus;
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}


mysql_free_result($result);
?>


Performance
Using both  mysql_fetch_array() and mysql_fetch_assoc()  is not significantly slower than using mysql_fetch_row(),
Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).
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).

0 comments:

Post a Comment