Why do I need to use echo to print MYSQL data to my table?
Take a look at my code sample.
what I am trying to do
Select and show all data in the query in a table.
Problem 1
This works: <td><?php echo "<br>". $row["testName"]. "<br>";?></td>
This does not: <td><?php $row["testName"] ?></td>
I feel the second option should work but does not.
(not such a big deal just feels wrong)
Problem 2
I would also like all data to be in one table not for the loop to create a new table every time.
$sql = "SELECT testName, userID FROM `results` WHERE `userID` = '$something' "; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { ?> <table class="table table-hover"> <thead> <tr> <th>ID</th> <th>Name</th> <th>some text Score</th> <th>Date Taken</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td><?php $row["testName"] ?></td> <td><?php $row["userID"] ?></td> <td><?php echo "<br>". $row["testName"]. "<br>";?></td> </tr> </tbody> </table> <?php } } else { echo "0 results"; }
<?php $row["testName"] ?>does nothing. You're not actuallyechoing it. If sort-tags are on, or in PHP higher than 5.4.0, you can do<?= $row["testName"] ?>, which echos it.echothe variable. 2. Move the first part and the last part of the table out of thewhileloop (to before and after it).<td><?= $row["testName"] ?></td>