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

24 Mar 2014

mysql_fetch_assoc in php and mysql

                       Mysql_fetch_assoc() return row as an associative array. Here, we can use numeric indices to get values from mysql data using php.

Consider following example:

                     mysql_fetch_assoc in php and mysql at phponwebsites

Retrieve data from table using mysql_fetch_assoc() in php:

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

        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());

           Now you'll get output like this:

mysql_fetch_assoc in php

            the result of array should be like this:
                  [game_ID] => 1
                  [category_Name] => Adventures
                  [game_Title] => Achilles

 Suppose you'll use

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

mysql_fetch_assoc at phponwebsites

              $res[0] represents the first column of mysql table. But you can't use this to retrieve data from table. Because you used mysql_fetch_assoc() mysql function to retrieve data.

Related Post: