Venkat
Venkat

Reputation: 1

How can i get the index value from the MySQL query with php?

Hi i am fetching the data from mysql data base i want get the index of the columns in the table

<?php 
//connects to database 
$con = mysql_connect("locahost","root","");
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("appulentoweb", $con); 

//retrieve data from database 
$result = mysql_query("SELECT * FROM questions"); 
?> 

i want get the index of the columns in the table then i will display that data in the screen.

in the table i have 2 columns i'e qno and titile i want display that titles in the output page

Thanks in Advance...

Upvotes: 0

Views: 4709

Answers (3)

Jalpesh Patel
Jalpesh Patel

Reputation: 3188

please try this..

   <?php  
        //connects to database  
        $con = mysql_connect("locahost","root",""); 
        if (!$con)  
        {  
        die('Could not connect: ' . mysql_error());  
        }  

        mysql_select_db("appulentoweb", $con);  

        //retrieve data from database  
        $result = mysql_query("SELECT * FROM questions"); ?>

        <table>
           <tr>
              <th> Question no</th>
              <th> Question </th>
           </tr>
        <?php 
        while($row=mysql_fetch_array($result))
        {
         ?>
         <tr>
            <td><?php echo $row['qno'];?></td>
            <td><?php echo $row['question'];?></td></tr>
  <?php } ?>
    </table>

i hope that it is help you....

if you have nay problem let me know...

Upvotes: 1

Idrees Khan
Idrees Khan

Reputation: 7752

try this;

 <?php  
//connects to database  
$con = mysql_connect("locahost","root",""); 
if (!$con)  
{  
die('Could not connect: ' . mysql_error());  
}  

mysql_select_db("appulentoweb", $con);  

//retrieve data from database  
$result = mysql_query("SELECT * FROM questions");  
    $rows = mysql_fetch_array($result, MYSQL_ASSOC);
    echo $rows['qno'];
    echo $rows['title'];

OR

$rows = mysql_fetch_array($result, MYSQL_NUM);
        echo $rows[0];
        echo $rows[1];

?>

Upvotes: 0

Mohit Singh
Mohit Singh

Reputation: 142

You can use function mysql_fetch_array which return indexed and associative array.

Upvotes: 0

Related Questions