0

i am having a Problem Displaying MySQL Records in a HTML Table

Here is the code:

    <html>
<head>
</head>
<body>
<?php
                        $con = mysql_connect("localhost", "root", "");
                        if (!con) {
                        die ("Can not connect: " . mysql_error());
                        }

                        mysql_select_db ("regform", $con);

                        $sql = "SELECT * FROM contacts";
                        $myData = mysql_query($sql, $con);
                      echo 'test';    
                        echo "<table border = '1'>

                        <tr>
                        <th>Name</th>
                        <th>Lastname</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>Comment</th>
                        </tr>";

                        while($record = mysql_fetch_array($myData)) {
                        echo "<tr>";
                        echo "<td>" . $record['Name'] . "</td>";
                        echo "<td>" . $record['Lastname'] . "</td>";
                        echo "<td>" . $record['Phone'] . "</td>";
                        echo "<td>" . $record['Email'] . "</td>";
                        echo "<td>" . $record['Comment'] . "</td>";
                        echo "</tr>";

                        }

                        echo "</table>";

                        mysql_close ($con);

                        ?>
</body>
</html>

i get this on the browser:

Name Lastname Phone Email Comment "; while($record = mysql_fetch_array($myData)) { echo ""; echo "" . $record['Name'] . ""; echo "" . $record['Lastname'] . ""; echo "" . $record['Phone'] . ""; echo "" . $record['Email'] . ""; echo "" . $record['Comment'] . ""; echo ""; } echo ""; mysql_close ($con); ?>

8
  • 2
    It's a .html page, right? Rename it to .php Commented Mar 22, 2013 at 19:58
  • Looks like your PHP isn't being parsed. Does the file end in .php? Are you getting any errors? Can you run a PHP page with just <?php phpinfo() ?>? Commented Mar 22, 2013 at 19:58
  • Sounds like your server doesn't have PHP installed or set-up. Commented Mar 22, 2013 at 19:59
  • what web server are you running? and do you have the correct modules installed (if you are running apache do you have mod-php)? Commented Mar 22, 2013 at 19:59
  • stackoverflow.com/questions/13944956/… Commented Mar 22, 2013 at 20:02

2 Answers 2

2

Your php is not being rendered by your web engine. It needs to have a *.php extension and be on a server that handles it.

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

1 Comment

Technically the pages don't have to end in .php as you can easily configure a web server to parse any extension as a PHP file.
-1

I guess it has to do with the multiline

             echo "<table border = '1'>

                    <tr>
                    <th>Name</th>
                    <th>Lastname</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Comment</th>
                    </tr>";

Perhaps you try this:

echo <<<END
<table border = '1'>

                    <tr>
                    <th>Name</th>
                    <th>Lastname</th>
                    <th>Phone</th>
                    <th>Email</th>
                    <th>Comment</th>
                    </tr>

END;

3 Comments

not true. As long as the quote is open, you can do multiple lines with an echo
Plus if that were the case it would error out, not render everything including the PHP.
Yes, this has nothing to do with the issue in hand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.