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:
Trying the following from the comments also didn't work and nothing got printed:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
CodePudding user response:
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>';
}
CodePudding user response:
You were mixing PDO and MYSQLI_ Also you had some debug code that would have damaged your table HTML.
Also there is no real point in moving data from a perfectly good array into scalar variables just to include those values in some HTML.
if ($result = $conn->query($query)) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>$row[judgeName]</td>
<td>$row[PC]</td>
<td>$row[TD]</td>
<td>$row[EX]</td>
<td>$row[ID]</td>
<td>$row[comment]</td>
</tr>";
}
}