数据库的表中,有name和password
在HTML的表格中,分开显示出name的值和password的值
如果通过PHP获取到数据库的数据后,怎么在HTML的表格中显示出

解决方案 »

  1.   

    例如显示name的值
    <?php foreach($pdo->query($sql) as $rows){?>
    <table>
    <tr>
    <td><input type="text" name="name" value="<?=$rows['name'] ?>" /></td>
    </tr>
    </table>
    <?php
    }
    ?>
      

  2.   


    // 创建数据库
    CREATE TABLE `user` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(20) NOT NULL,  `password` varchar(100) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    插入表数据INSERT INTO  `test`.`user` (`id` ,`name` ,`password`)VALUES (NULL ,  'fdipzone',  '123');
    INSERT INTO  `test`.`user` (`id` ,`name` ,`password`)VALUES (NULL ,  'csdn',  '456');
    php代码<?php    //打开数据库
        function opendb(){
            $conn=@mysql_connect("localhost","root","")  or die(mysql_error());
            @mysql_select_db('test',$conn) or die(mysql_error());   
        }
        opendb();    echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';    $sqlstr = "select * from user";
        $query = mysql_query($sqlstr) or die(mysql_error());    while($thread=mysql_fetch_assoc($query)){
            $result[] = $thread;
        }    if($result){
            echo '<table><tr><td>name</td><td>password</td></tr>';
            foreach($result as $val){
                echo '<tr><td>'.$val['name'].'</td><td>'.$val['password'].'</td></tr>';
            }
            echo '</table>';
        }?>
      

  3.   

    首先.html格式的文件不能解析php,要输出php变量必须是.php后缀的文件。
    输出方法$password;
    $username; //假设你已经得到这两个变量了<html>
    <body>
    <table>
    <tr><?php echo $username; ?><//tr>
    <tr><?php echo $password; ?><//tr>
    </table>
    </body>
    </html>