1

I want to display my database into my webpage in tables. but it cant, i pretty sure that my database name is same. if i run, ".$no.", ".$row['nama'].", ".$row['email']." and ".$row['message']." will displayed on webpage. not data from database. please help.

here is my code:

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

the error will be like this: enter image description here

this is my 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();
}
?>
10
  • Please write Error message Commented May 1, 2016 at 13:01
  • 3
    Are you sure your php-code is executed? Commented May 1, 2016 at 13:06
  • just test this code <?php $num = 1; echo $num; ?> it will display 1 Commented May 1, 2016 at 13:07
  • i sure, because if i try that php-code in other program, it will work Commented May 1, 2016 at 13:12
  • 1
    you are mixing mysql with mysqli Commented May 1, 2016 at 13:15

3 Answers 3

2

You are mixing mysql_* with mysqli_*. Please don't do that. Check the below code (changes are commented):-

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

Note:- this code file extension must be .php.

Sign up to request clarification or add additional context in comments.

Comments

2

You forget a i. Change mysql_query() to mysqli_query() and mysql_fetch_array() to mysqli_fetch_array().

Comments

0

Chnage conection.php. Change all mysqli methods to 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.');;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.