1

I'm trying to display data from a MySQL table in a html table using php, I've looked at a few tutorials online including answers on StackOverflow... I've implemented it the way said tutorials have described but I am getting no output.

<table border="1px solid black" cellpadding="0px" cellspacing="0px" width="100%">
        <thead>
            <tr>
                <th>Date</th>
                <th>Monday</th>
                <th>Tuesday</th>
                <th>Wednesday</th>
                <th>Thursday</th>
                <th>Friday</th>
                <th>Saturday</th>
                <th>Sunday</th>
            </tr>
        </thead>
            <?php
                include('dbConnect.php');
                $sql = "SELECT * FROM nurses";
                $result = mysql_query($sql);

                while($row = mysql_fetch_assoc($result)) {
                    echo "<tr>";
                    echo "<td>" . $row['idno'] . "</td>";
                    echo "<td>" . $row['surname'] . "</td>";
                    echo "</tr>";
                }
            ?>
</table>

I know that my db connection is successful as I test for this. All that's getting output is the <thead> and then nothing. I don't understand why this isn't working :/

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
Gethin
  • 316
  • 3
  • 15
  • 1
    mysql_* functions are deprecated by the way, stick with mysqli_* – ksealey Dec 16 '14 at 00:44
  • All that;s getting outputted is the "header row", I assume you mean? – Tim Dearborn Dec 16 '14 at 00:45
  • 1
    Connection being what; `mysqli_`, `mysql_`, PDO? Do this `$result = mysql_query($sql) or die(mysql_error());` and use http://php.net/manual/en/function.error-reporting.php - Plus, is your file `.php` or `.html`? – Funk Forty Niner Dec 16 '14 at 00:51
  • 1
    *"If possible, could somebody help me with a new related problem?"* - Sure, [click here](http://stackoverflow.com/questions/ask). You shouldn't modify your question with it being solved and asking what should be a new question. I did a rollback to the original post. – Funk Forty Niner Dec 16 '14 at 00:58
  • @Fred-ii- yeah thanks that's how I managed to fix it :) – Gethin Dec 16 '14 at 00:58
  • ok @Fred-ii- I'll make a new question... just gonna have to wait 90 minutes unfortunately :/ although maybe I can figure it before then... just maybe. – Gethin Dec 16 '14 at 01:00
  • 1
    Unfortunately, that's how the Stack system works. ;) I saved you from someone flagging your question ;) – Funk Forty Niner Dec 16 '14 at 01:01
  • @Fred-ii- okay thanks :) getting used to how stackoverflow works too... I'll get googling in the meantime! – Gethin Dec 16 '14 at 01:05

1 Answers1

3

If you say you have tested this, then there should be no error when trying to execute mysql_query. To be sure that any data is being fetched though, please do a var_dump(mysql_fetch_assoc($result)) after the $result = mysql_query($sql); line to see if at least a single line is being returned from the database. Additionally, you should not use mysql_* functions anymore since they are being deprecated. Check here how it works. http://php.net/manual/en/function.mysql-query.php

In the case that var_dump returns at least a single result, then most likely $row['idno'] is not a proper column name in your results (you can see what is in the array from the var_dump)

ziGi
  • 828
  • 8
  • 28
  • Thanks @zigi I did indeed have an error somewhere in my connection, not sure how I was able to echo that the connection was successful. – Gethin Dec 16 '14 at 00:51
  • 1
    Please don't forget to select a correct answer/vote. Additionally, I strongly suggest that you start using PDO instead of `mysql` methods. Check here: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – ziGi Dec 16 '14 at 00:52
  • Here is also a really good SO article about why you should not use it http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – ziGi Dec 16 '14 at 01:34