dragon8837 2016-02-05 01:36
浏览 216
已采纳

网页不显示数据库内容

Iam trying to display contents from the database. No values are getting displayed. Why does my code not work? I have xampp server running too !

<html>
<body>
<?php$username="";$password="";$database="login";
mysql_connect("localhost:170",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM logindetails";
$result=mysql_query($query);
$num=mysql_numrows($result);mysql_close();?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<font face="Arial, Helvetica, sans-serif">Value1</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value2</font>
</td>
</tr>
<?php$i=0;while ($i < $num) {$f1=mysql_result($result,$i,"Username");
$f2=mysql_result($result,$i,"Email");?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo '$f1'; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo '$f2'; ?></font>
</td>

</tr>
<?php$i++;}?>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongxiong5546 2016-02-05 02:04
    关注

    I believe the 'mysql_connect' has been deprecated from PHP 5.5 and if you're running PHP 7.0 is has been removed. You will want to use MySQLi or PDO.

    I do not believe there is much difference between the two other than PDO can connect to other databases other than MySQL, whereas, MySQLi can only connect to MySQL.

    As to helping you fix your code. This should be close to what you need. I hope it helps

    <html>
    <body>
    <?php
    $username="root";
    $password="password"; // 'password' set to whatever it is on your DB
    $database="login";
    
    $dbc = @mysqli_connect("localhost:170",$username,$password)
        OR die( "Unable to select database");
    
    
    $query="SELECT * FROM logindetails";
    
    $result=mysqli_query($dbc, $query);
    
    $row= mysqli_fetch_array($result);
    
    mysqli_close();
    ?>
    <table border="0" cellspacing="2" cellpadding="2">
    <tr>
    <td>
    <font face="Arial, Helvetica, sans-serif">Value1</font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif">Value2</font>
    </td>
    </tr>
    <?php
        //$i=0;while ($i < $num) {$f1=mysql_result($result,$i,"Username");
        //$f2=mysql_result($result,$i,"Email");
    ?>
    <tr>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo "$row['Username']"; ?></font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo "$row['Email']"; ?></font>
    </td>
    
    </tr>
    <?php
        //$i++;}
    ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答