0

I have a SQL table and I would like to display it in a HTML page. My code is displaying a table but the content is empty although my table is filled with data.

    <?php
        ini_set('display_errors', 'On');
        error_reporting(E_ALL);

        $database = 'karin';
        $connect = mysql_connect('localhost','root', 'karin');
        if (!$connect) {
            die(mysql_error());
        }
        if (!mysql_select_db($database))
            die("Can't select database");

        $results = mysql_query("SELECT * FROM service_messages");
        if (!$results) {
            die("Query to show fields from table failed");
        }


        while($row = mysql_fetch_array($results)) {
        ?>
            <tr>
                <td><?php echo $row['partner']?></td>
                <td><?php echo $row['GUID']?></td>
                <td><?php echo $row['serviceName']?></td>
                <td><?php echo $row['serviceID']?></td>
                <td><?php echo $row['countries']?></td>
                <td><?php echo $row['rate']?></td>
                <td><?php echo $row['ips']?></td>
                <td><?php echo $row['callback']?></td>
            </tr>

        <?php
        }
        ?>
        </tbody>
        </table>
</body>

Why doesn't it show the table content? Thanks!

4
  • check echo count($row); Commented Nov 8, 2015 at 13:38
  • var_dump() your $row to see if there is something in the result Commented Nov 8, 2015 at 13:40
  • mysql_select_db expect two(2) parameters right ? mysql_select_db('YourDB', $connection) Commented Nov 8, 2015 at 14:22
  • I don't think $results receives any value for some reason... $result = mysqli_query("SELECT * FROM service_messages"); Commented Nov 8, 2015 at 14:27

3 Answers 3

2

1). Don't use mysql_*. Please use PDO or MySQLi.

2). I don't find any <table> (table starting tag) anywhere in your code.

3). Use mysql_fetch_assoc instead of mysql_fetch_array.

Here's the updated code.

    $database = 'karin';
    $connect = mysql_connect('localhost','root', 'karin');
    if (!$connect) {
        die(mysql_error());
    }
    if (!mysql_select_db($database))
        die("Can't select database");

    $results = mysql_query("SELECT * FROM service_messages");
    if (!$results) {
        die("Query to show fields from table failed");
    }
     ?>
    <table>
    <?php
    while($row = mysql_fetch_assoc($results)) {
    ?>
        <tr>
            <td><?php echo $row['partner']?></td>
            <td><?php echo $row['GUID']?></td>
            <td><?php echo $row['serviceName']?></td>
            <td><?php echo $row['serviceID']?></td>
            <td><?php echo $row['countries']?></td>
            <td><?php echo $row['rate']?></td>
            <td><?php echo $row['ips']?></td>
            <td><?php echo $row['callback']?></td>
        </tr>

    <?php
    }
    ?>
    </tbody>
    </table>

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

Comments

0

Try this

 while($row = mysql_fetch_row($results)) {
        ?>
            <tr>
                <td><?php echo $row['partner']?></td>
                <td><?php echo $row['GUID']?></td>
                <td><?php echo $row['serviceName']?></td>
                <td><?php echo $row['serviceID']?></td>
                <td><?php echo $row['countries']?></td>
                <td><?php echo $row['rate']?></td>
                <td><?php echo $row['ips']?></td>
                <td><?php echo $row['callback']?></td>
            </tr>

        <?php
        }
        ?>

For more refrence : http://php.net/manual/en/function.mysql-fetch-row.php

Comments

0

Well, for some reason it works like this:

    <?php
    //ini_set('display_errors', 'On');
    //error_reporting(E_ALL);

    $mysqli = new mysqli("localhost", "root", "2014", "cd");

    $q = $mysqli->query("select * from service_messages");

    while($row = $q->fetch_assoc()){
        $arr[] = $row;
    }
    echo json_encode($arr);



?>

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.