0

嗨,我正在从 mysql 数据库中获取数据,我想获取表中列的索引

<?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"); 
?> 

我想获取表中列的索引,然后我将在屏幕上显示该数据。

在表中我有 2 列,即 qno 和标题我想在输出页面中显示标题

提前致谢...

4

3 回答 3

1

请试试这个..

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

我希望它对你有帮助......

如果您有任何问题,请告诉我...

于 2012-07-18T06:50:29.490 回答
0

试试这个;

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

或者

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

?>
于 2012-07-18T06:43:22.690 回答
0

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

于 2012-07-18T06:47:21.177 回答