0

I have this code:

<?php
$dbhost = 'localhost';
$dbuser = 'MyUsername';
$dbpass = 'MyPassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT contactID, name, phone, email, choise, message
        FROM contact2';

mysql_select_db('nghiartc_contact');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

    echo "<td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td>";        
} 

mysql_close($conn);
?>

I'm trying to display the data from the database like this:

enter image description here

But right now I get:

enter image description here

Any idea how I need to modify the code, so I get my expected output?

B8vrede
  • 4,432
  • 3
  • 27
  • 40
  • You need to tell the browser where to place the table using `` and what a row is with ``. If you do that your code will work fine :) Because the current output is just all the cells on a single row.
    – B8vrede Mar 26 '16 at 06:36
  • You can take a [tour] here and get a quick overview of this site. – Rizier123 Mar 26 '16 at 07:39

3 Answers3

0

you are missing the table structure iteslf - try adding the <table> and <tr> tags. Then you can style it accordingly with borders etc.

echo"<table>";
//echo"<tr><th>...table header structure...</th></tr>";

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr><td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td></tr>";        
} 
echo"</table>";
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

Please see that you haven't followed the proper HTML structure. You should be using and tags.

For example,

echo "<table border='1'>";

while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
   echo "<tr>";
   echo "<td>{$row['contactID']} </td><td>{$row['name']}</td><td>  {$row['phone']} </td><td>{$row['email']}</td> <td>{$row['choise']}</td> <td>{$row['message']}</td>"; 
   echo "</tr>";      
} 
echo "</table>";

Hope this helps.

Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

You are missing table tags to start your table and tr tags for each row of data, so you would have to modify your while loop to this:

echo "<table>";
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{

    echo "<tr>";
    echo "<td>{$row['contactID']}</td>
          <td>{$row['name']}</td>
          <td>{$row['phone']}</td>
          <td>{$row['email']}</td>
          <td>{$row['choise']}</td>
          <td>{$row['message']}</td>";    
    echo "</tr>";    
} 
echo "</table>";

But please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi.

So I would highly recommend to change your code to PDO, read more in the manual, an as example:

<?php

    $dbhost = 'localhost';
    $dbuser = 'nghiartc_contact';
    $dbpass = 'ZP5IEVeyKx';

    try {

        $dbh = new PDO("mysql:host=$dbhost;dbname=nghiartc_contact", $dbuser, $dbpass);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $result = $dbh->query("SELECT contactID, name, phone, email, choise, message FROM contact2");

    } catch(PDOException $e) {
        echo $e->getMessage();
    }


    echo "<table>";
    foreach($result->fetchAll() as $row){
        echo "<tr>";
            echo "<td>" . $row["contactID"] . "</td>";
            echo "<td>" . $row["name"] . "</td>";
            echo "<td>" . $row["phone"] . "</td>";
            echo "<td>" . $row["email"] . "</td>";
            echo "<td>" . $row["choise"] . "</td>";
            echo "<td>" . $row["message"] . "</td>";
        echo "</tr>";
    }
    echo "</table>";

    $dbh = NULL;

?>
Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156