Skip to content
Advertisement

showing contents of a table using PHP in HTML

I have this code snippet:

try 
{
    $conn = new PDO("mysql:host=$host;dbname=judges", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo "Connection failed: " . $e->getMessage();
}

and

$query = "SELECT * FROM feedback";


echo '<table border="0" cellspacing="2" cellpadding="2"> 
      <tr> 
          <td> <font face="Arial">Judge Name</font> </td> 
          <td> <font face="Arial">PC</font> </td> 
          <td> <font face="Arial">TD</font> </td> 
          <td> <font face="Arial">EX</font> </td> 
          <td> <font face="Arial">ID</font> </td> 
          <td> <font face="Arial">comment</font> </td>
      </tr>';

if ($result = $conn->query($query)) {
    echo "got result";

    echo $result["PC"];
    //while ($row = $result->fetch_assoc()) {
    while ($row = $result) {
        $field1name = $row["judgeName"];
        $field2name = $row["PC"];
        $field3name = $row["TD"];
        $field4name = $row["EX"];
        $field5name = $row["ID"]; 
        $field6name = $row["comment"];
 
        echo '<tr> 
                  <td>'.$field1name.'</td> 
                  <td>'.$field2name.'</td> 
                  <td>'.$field3name.'</td> 
                  <td>'.$field4name.'</td> 
                  <td>'.$field5name.'</td> 
                  <td>'.$field6name.'</td> 
              </tr>';
    }
} 

both inside submit.php, however, while the feedback table inside judges database is not empty, I don’t see any of the rows shown in the HTML page only the column name shows up in the Response tab of Network tab in Firefox Inspect tool. What do you suggest to populate the existing database into the html page even before I enter the information for the new user?

Here’s my feedback table from the judges database:

enter image description here

Trying the following from the comments also didn’t work and nothing got printed:

while ($row = $result->fetch(PDO::FETCH_ASSOC)) {

enter image description here

Advertisement

Answer

There’s no point in the if statement. You have PDO::ERRMODE_EXCEPTION enabled, so if there’s an error in the query it will throw an exception, not return false.

The syntax to fetch a row in PDO is $result->fetch(PDO::FETCH_ASSOC).

$result = $conn->query($query);
echo "<td><tr>got result</tr></td>";

while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $field1name = $row["judgeName"];
    $field2name = $row["PC"];
    $field3name = $row["TD"];
    $field4name = $row["EX"];
    $field5name = $row["ID"]; 
    $field6name = $row["comment"];
 
    echo '<tr> 
             <td>'.$field1name.'</td> 
             <td>'.$field2name.'</td> 
             <td>'.$field3name.'</td> 
             <td>'.$field4name.'</td> 
             <td>'.$field5name.'</td> 
             <td>'.$field6name.'</td> 
         </tr>';
} 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement