0

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";
}
4
  • 1
    <?php $row["testName"] ?> does nothing. You're not actually echoing it. If sort-tags are on, or in PHP higher than 5.4.0, you can do <?= $row["testName"] ?>, which echos it. Commented Nov 3, 2016 at 12:16
  • 1. You forgot to echo the variable. 2. Move the first part and the last part of the table out of the while loop (to before and after it). Commented Nov 3, 2016 at 12:16
  • 1
    Change your second option to <td><?= $row["testName"] ?></td> Commented Nov 3, 2016 at 12:16
  • Thanks guys, ill do that Commented Nov 3, 2016 at 12:19

3 Answers 3

1

If you want to display value from database then you must need to use echo. because $row["testName"] contains some value but which value it is, that you can see after echoing it.

To display data in single table try below code:

    <?php 
$sql = "SELECT testName, userID FROM  `results` WHERE  `userID` = '$something' ";
      $result = $conn->query($sql); ?>
      <table class="table table-hover">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>some text Score</th>
                <th>Date Taken</th>
            </tr>
        </thead>
    <?php

        if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
    ?>
        <tr>
            <th scope="row">1</th>
            <td><?php echo $row["testName"] ?></td>
            <td><?php echo $row["userID"] ?></td>
            <td><?php echo "<br>". $row["testName"].  "<br>";?></td>
        </tr>

    <?php } ?> 
 </table>
   <?php }else 
       {
         echo "0 results";
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Plus 1 and check, nice quick fix
Thanks Keep helping :)
1

Run while like this

  <?php
  while($row = $result->fetch_assoc()) { ?>
  <tr>
         <th scope="row">1</th>
         <td><?php  echo $row["testName"] ?></td>
         <td><?php  echo $row["userID"] ?></td>
         <td><?php echo "<br>". $row["testName"].  "<br>";?></td>
  </tr>
  <?php } ?>

Comments

1

Description

void echo ( string $arg1 [, string $... ] )

Outputs all parameters. No additional newline is appended.

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.

Ref : http://php.net/manual/en/function.echo.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.