错误:将数据表格数据库显示到网页

时间:2016-05-01 12:55:34

标签: php html

我想在表格中将我的数据库显示在我的网页中。但它不能,我很确定我的数据库名称是相同的。如果我投放,则会在网页上显示".$no."".$row['nama']."".$row['email']."".$row['message']."。不是数据库中的数据。请帮忙。

这是我的代码:

<table class="table">
    <thead>
    <tr>
     <th>#</th>
     <th>Nama</th>
     <th>E-mail</th>
     <th>Comment</th>
    </tr>
    </thead>
     <tbody>
        <tr>
          <td>1</td>
          <td>Mark</td>
          <td>Otto</td>
          <td>@mdo</td>
        </tr>
     </tbody>
<?php 
include "conection.php";
$no = 1;
$query = mysql_query("SELECT * FROM comment");
if ($query) {
    while ($row = mysql_fetch_array($query)) {
        echo "
        <tr>
            <td>".$no."</td>
            <td>".$row['nama']."</td>
            <td>".$row['email']."</td>
            <td>".$row['message']."</td>
        </tr>
        ";

    $no++;
    }
}
?>
</table>

错误将是这样的: enter image description here

这是我的conection.php

<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "toefl";

$koneksi = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if(mysqli_connect_error()){
    echo 'database error : '.mysqli_connect_error();
}
?>

3 个答案:

答案 0 :(得分:2)

你忘记了我。将mysql_query()更改为mysqli_query(),将mysql_fetch_array()更改为mysqli_fetch_array()

答案 1 :(得分:2)

您正在将mysql_*mysqli_*混合。请不要这样做。检查以下代码(更改已注释): -

<table class="table">
    <thead>
    <tr>
     <th>#</th>
     <th>Nama</th>
     <th>E-mail</th>
     <th>Comment</th>
    </tr>
    </thead>
     <tbody><!-- check the change -->
<?php 
include "conection.php";
$no = 1;
$query = mysqli_query($koneksi,"SELECT * FROM comment"); // don't mix `mysql_*` with `mysqli_*`
if ($query) {
    while ($row = mysqli_fetch_array($query)) {// don't mix `mysql_*` with `mysqli_*`
        echo "<tr><td>".$no."</td><td>".$row['nama']."</td><td>".$row['email']."</td><td>".$row['message']."</td></tr>";
        $no++;
    }
}
?>
</tbody>
</table>

注意: - 此代码文件扩展名必须为.php

答案 2 :(得分:0)

Chnage conection.php。将所有mysqli方法更改为mysql

<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "toefl";

$koneksi = mysql_connect($db_host, $db_user, $db_pass);

if (!$koneksi) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully'
mysql_select_db($db_name, $koneksi) or die('Could not select database.');;
?>