简体   繁体   中英

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...

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...

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];

?>

您可以使用函数mysql_fetch_array返回索引数组和关联数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM