我想在表格中将我的数据库显示在我的网页中。但它不能,我很确定我的数据库名称是相同的。如果我投放,则会在网页上显示".$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>
这是我的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();
}
?>
答案 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.');;
?>