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); ?>

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Manos
  • 1,471
  • 1
  • 28
  • 45
  • 2
    It's a `.html` page, right? Rename it to `.php` – Unamata Sanatarai Mar 22 '13 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 ``? – j08691 Mar 22 '13 at 19:58
  • Sounds like your server doesn't have PHP installed or set-up. – Dai Mar 22 '13 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)? – m79lkm Mar 22 '13 at 19:59
  • http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli – Alex W Mar 22 '13 at 20:02
  • Yeah renamed it to .php and it worked. Now i get new error that i'll try to fix Notice: Use of undefined constant con - assumed 'con' in D:\xampp\htdocs\project2\thankyou.php on line 7 test Notice: Undefined index: Name in on line 28 Notice: Undefined index: Name in on line 28 – Manos Mar 22 '13 at 20:05
  • if the table name in the database is not `Name` (notice the capital 'N') then change it to how it is in the database.. also it's `$con` not `con`. I suggest learning more of the basics of PHP – UnholyRanger Mar 22 '13 at 20:06
  • Yeah i am new to php, studying about 15 days... I fixed all the errors Thanks a lot for your help !!!!!!!! – Manos Mar 22 '13 at 20:10

2 Answers2

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.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • 4
    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. – j08691 Mar 22 '13 at 20:04
-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;

karthikr
  • 97,368
  • 26
  • 197
  • 188