PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

26 Mar 2014

mysql_fetch_row in php and mysql

                      Mysql_fetch_row()  return row as numeric array. We can't use associative indices to retrieve data from mysql table using php.

Consider following example:

                     mysql_fetch_row in php and mysql at phponwebsites

Retrieve data from table using mysql_fetch_row()  in php:

                       You can retrieve data from mysql table using  mysql_fetch_assoc() in php. The following php script is used for retrieve data.

<html>
<body>
<table>
<th>Game_ID</th><th>Category</th><th>Title</th>
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$query=mysql_query('select * from table1') or die(mysql_error());
while($res=mysql_fetch_row($query))
{
  echo'<tr><td>'.$res[0].'</td><td>'.$res[1].'</td><td>'.$res[2].'</td></tr>';
}
echo'<table>';
?>
</body>
</html>

           Now you'll get output like this:

mysql_fetch_row() in php

Where,
          the result of array should be like this:
             Array(
                         [0] => 1
                         [1] => Adventures
                         [2] => Achilles
                       )      

 Suppose you'll use following php script,
echo'<tr><td>'.$res[0].'</td><td>'.$res[1].'</td><td>'.$res['game_Title'].'</td></tr>';

That means you use associative index instead numerical, then you will get undefined index error like as follows as:

mysql_fetch_row at phponwebsites
 Where,
              'game_Title' is the field name of column. You can't use $res['game_title'] here. Because you retrieved data from table using mysql_fetch_row().

Related Post: