Difference between mysql_fetch_array and mysql_fetch_assoc in PHP

2,051 views

Published on

Using this pdf one can easily understand difference between ways of fetch data from database in PHP.

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,051
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Difference between mysql_fetch_array and mysql_fetch_assoc in PHP

  1. 1. Vineet Kumar SainiDifference between mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() andmysql_fetch_object() in phpMany of the php programming newbies get confused about mysql_fetch_array(),mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() functions, but all of thesefunctions performs a similar process.Let us create a table “tb” for clear example with three fields “id”, “username” and “password”Table: tbInsert a new row into the table with values 1 for id, tobby for username and tobby78$2 forpassworddb.php<?php$query=mysql_connect("localhost","root","");mysql_select_db("tobby",$query);?> 1. mysql_fetch_row()Fetch a result row as an numeric array<html><?phpinclude(db.php);$query=mysql_query("select * from tb");$row=mysql_fetch_row($query);echo $row[0];echo $row[1];echo $row[2];?></html>Result1 tobby tobby78$2 www.vineetsaini.wordpress.com
  2. 2. Vineet Kumar Saini 2. mysql_fetch_object()Fetch a result row as an object<html><?phpinclude(db.php);$query=mysql_query("select * from tb");$row=mysql_fetch_object($query);echo $row->id;echo $row->username;echo $row->password;?></html>Result1 tobby tobby78$2 3. mysql_fetch_assoc()Fetch a result row as an associative array<html><?phpinclude(db.php);$query=mysql_query("select * from tb");$row=mysql_fetch_assoc($query);echo $row[id];echo $row[username];echo $row[password];?></html>Result1 tobby tobby78$2 www.vineetsaini.wordpress.com
  3. 3. Vineet Kumar Saini 4. mysql_fetch_array()Fetch a result row as an associative array, a numeric array and also it fetches by both associative& numeric array.<html><?phpinclude(db.php);$query=mysql_query("select * from tb");$row=mysql_fetch_array($query);echo $row[id];echo $row[username];echo $row[password];<span style="color: #993300;">/* here both associative array andnumeric array will work. */</span>echo $row[0];echo $row[1];echo $row[2];?></html>Result1 tobby tobby78$2 www.vineetsaini.wordpress.com
  4. 4. Vineet Kumar SainiWays to retrieve the data in the result set of MySQL using PHP1. mysql_fetch_row($result):-where $result is the result resource returned from a successful query executed using themysql_query() function.Example:$result = mysql_query(“SELECT * from students);while($row = mysql_fetch_row($result)){ Some statement;}2. mysql_fetch_array($result):- Return the current row with both associative and numeric indexes where each column can eitherbe accessed by 0, 1, 2, etc., or the column name.Example:$row = mysql_fetch_array($result)3. mysql_fetch_assoc($result):- Return the current row as an associative array, where the name of each column is a key in thearray.Example:$row = mysql_fetch_assoc($result)$row[‘column_name’]4.mysql_fetch_object($result):-Results are objects returned from database. Fields are accessible like$result->name, $result->cust_name, where $result is the result object and name, cust_name arethe fields.mysql_fetch_array : Results are arrays returned from database. Fields are accessible like$result[name], $result[cust_name]. www.vineetsaini.wordpress.com

×