1

I am relatively new to PHP programming. I am building a small crud app (to-do list) to be exact for practice. I am working on fleshing out the UI and testing how information displays from my database on the page.

When a user logs in, they are shown a table of all the items they have saved.

table listing items

  <div class="container">
    <table class="table">
      <tr>
        <th>Completed</th>
        <th>Description</th>
        <th>Actions</th>
      </tr>
      <tr>
        <td colspan="3"></td>
      </tr>
      <?php
      $document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
      while($match_value = mysql_fetch_array($document_get)) {
      ?>
      <tr>
        <td>
          Hi
        </td>
      </tr>
    </table>
    <?php
    }
    ?>
  </div>

php call to display list item

      <?php
      $document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
      while($match_value = mysql_fetch_array($document_get)) {
      ?>

Anything under this PHP request does not show up for some reason. I have looked at other posts where people have mentioned the same thing but I did not see anything specific that matched my issue per say.

Marty Lavender
  • 105
  • 1
  • 12
  • 3
    Several things: 1. This code is vulnerable to [SQL Injections](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496). This is bad. Don't use `mysql_` functions, they are deprecated. If you're on PHP 7, they are removed and this is the problem. 2. Check [this](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) to debug your problem. Are you sure the last `` is not being displayed? If it is not, you have a syntax error. If it is, there is simply not data to display. – Imanuel Jul 07 '17 at 20:52
  • 1
    on another note your while loop should end after not after – Just_Do_It Jul 07 '17 at 20:57
  • Thanks for the comment. The example that I was starting with did in fact run on PHP 5.6. I noticed quite a few things that did not look correct when looking at newer documentation. I am changing things around so I will be using PHP 7 instead. The last DIV was not being displayed i.e., the DIV that simply says 'Hi' – Marty Lavender Jul 07 '17 at 22:29

3 Answers3

0

mysql_fetch_array returns an array. The structure of your while statement will not have the outcome that you desire. The while loop will continue to be true forever because nothing is changing in the while condition.

This should have the effect closer to what you are looking for:

$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
$match_value = mysql_fetch_array($document_get);
foreach ($match_value as $value){
    //Do something
}

As a side note - I would highly recommend using PDO and not putting php in-line with html. Tutorial here: https://phpdelusions.net/pdo

If you use PDO your loop would look more like this:

while($row = $st->fetch(PDO::FETCH_ASSOC)){
    //Do something
}
Andrew Cameron
  • 73
  • 1
  • 12
  • the while is not the problem. actually it is required to retrieve the result row by row (same as you suggest with PDO). mysql_fetch_array() just get's the row's columns as associative and not as numerical array. – masterfloda Jul 07 '17 at 21:42
0
enter code here  <?php
  $document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");
  while($match_value = mysql_fetch_array($document_get)) {
  echo "
  <tr>
    <td>
      Hi
    </td>
  </tr>

";

}
echo    " </table>";
?>

Do the whole thing in php using echo to display the table elemts and Hi. Remove the extra php tags so loop and echo all php. Also look at security issues mentioned in comments. By the way, I moved the table end tag outside loop after reading comment below the question.

Lew Perren
  • 1,209
  • 1
  • 10
  • 14
0

Try to alter your query from:

$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='$user_id' ORDER BY id DESC");

to

$document_get = mysql_query("SELECT * FROM todolist WHERE user_id='".$user_id."' ORDER BY id DESC");
Just_Do_It
  • 821
  • 7
  • 20