This is a simple question for PHP users. The reason I couldn\'t get the the exact difference between mysql_fetch_array()
and mysql_fetch_row()
in P
The mysql_fetch_object{row/array/assoc}() function collects the first single matching record in its respective format and can be retrieved accordingly.
con.php
<?php
$host = $_GET['host'];
$username = $_GET['username'];
$pass = $_GET['pass'];
$database = $_GET['database'];
$connect=new connect($host,$username,$pass,$database);
class connect{
function __construct($host,$user,$password,$db_name){
mysql_connect($host,$user,$password) or die("Connection error");
mysql_select_db($db_name);
$error=mysql_error();
if (!empty($error))
{
echo $error;
}
}
}
?>
index.php
<?php
$query=mysql_query("select * from category");
?>
How each value will look on dumping the array in browser
mysql_fetch_array
$row=mysql_fetch_array($query);
var_dump($row);
Output:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string '1' (length=1)
'createdBy' => string '1' (length=1)
2 => string 'APTITUDE' (length=8)
'catName' => string 'APTITUDE' (length=8)
3 => string 'APTITUDE' (length=8)
'description' => string 'APTITUDE' (length=8)
4 => string '1' (length=1)
'status' => string '1' (length=1)
mysql_fetch_row
$row=mysql_fetch_row($query);
var_dump($row);
Output:
array
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'APTITUDE' (length=8)
3 => string 'APTITUDE' (length=8)
4 => string '1' (length=1)
mysql_fetch_assoc
$row=mysql_fetch_assoc($query);
var_dump($row);
Output:
array
'id' => string '1' (length=1)
'createdBy' => string '1' (length=1)
'catName' => string 'APTITUDE' (length=8)
'description' => string 'APTITUDE' (length=8)
'status' => string '1' (length=1)
mysql_fetch_object
$row=mysql_fetch_object($query);
var_dump($row);
Output:
object(stdClass)[2]
public 'id' => string '1' (length=1)
public 'createdBy' => string '1' (length=1)
public 'catName' => string 'APTITUDE' (length=8)
public 'description' => string 'APTITUDE' (length=8)
public 'status' => string '1' (length=1)
Rest @Gaurang has provided how to use the out for your code and other activities.
Mysql_fetch_object returns the result from the database as objects while mysql_fetch_array returns result as an array. This will allow access to the data by the field names.
Fetch row returns a numerical array for current entry
http://www.php.net/manual/en/function.mysql-fetch-row.php
Fetch array will by default return a full id=>key=>value array but it also offers the option of choosing either numerical or associative return
http://www.php.net/manual/en/function.mysql-fetch-array.php
There are 3 functions. mysql_fetch_assoc mysql_fetch_row mysql_fetch_array (a combination of row and assoc)
I would recommend _assoc or _row to optimize your code and keep it clear. If you're grabbing a single column, use $row = mysql_fetch_row $row[0]
The documentation is pretty clear on this, have you looked at it ?
mysql_fetch_array ( resource $result [, int $result_type = MYSQL_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_type is 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), [by] using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
mysql_fetch_row ( resource $result )
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. Each result column is stored in an array offset, starting at offset 0.
In summary
mysql_fetch_array( $result, MYSQL_ASSOC )
= mysql_fetch_assoc( $result )
mysql_fetch_array( $result, MYSQL_NUM )
= mysql_fetch_row( $result )
And
mysql_fetch_array ( $result )
= mysql_fetch_assoc( $result )
+ mysql_fetch_row( $result )
mysql_fetch_array as the manual says can return an int (position) based index, associative array or both according to the result_type chosen.
in the other hand mysql_fetch_row always return the result set based on integer index.
I personally recommend you to use mysql_fetch_array passing MYSQL_ASSOC as second parameter since is always easier to know what field you would like to fetch