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

8
  • 1
    mysql_* functions are deprecated by the way, stick with mysqli_* Commented Dec 16, 2014 at 0:44
  • All that;s getting outputted is the "header row", I assume you mean? Commented Dec 16, 2014 at 0:45
  • 1
    Connection being what; mysqli_, mysql_, PDO? Do this $result = mysql_query($sql) or die(mysql_error()); and use php.net/manual/en/function.error-reporting.php - Plus, is your file .php or .html? Commented Dec 16, 2014 at 0:51
  • 1
    "If possible, could somebody help me with a new related problem?" - Sure, click here. 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. Commented Dec 16, 2014 at 0:58
  • 1
    Unfortunately, that's how the Stack system works. ;) I saved you from someone flagging your question ;) Commented Dec 16, 2014 at 1:01

1 Answer 1

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)

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

3 Comments

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.
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: wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Here is also a really good SO article about why you should not use it stackoverflow.com/questions/12859942/…

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.