Home > database >  Using php and the while loop to print a table of data that is being selected from a mysql db [duplic
Using php and the while loop to print a table of data that is being selected from a mysql db [duplic

Time:09-30

I am new at php and am trying to learn CRUD applications. For this part of the code I have a table set up in a database (mysql). I am trying to interact with the table. I want a user to login (that part works) and when they are logged in- I store a message in $_SESSION that says ie. 'success'. This lets me know the user is logged in. Not the problem. The second piece is that IF there are rows IN THE TABLE already (from previous sessions) I want to print out these rows in a table. If there are no rows I want to print out "No rows"

I am trying to use simple logic here where first I create the $row variable by doing a fetch that comes from a pdo object(also fine). If that row is TRUE ie. it exists, print the table. If that row is false, print no rows. However, I keep getting the whole table printed, say 5 times. Instead of the 5 rows in the table.

Also, I am not sure I am using the correct type of while () statement. Which right now reads,
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))

And then I get the WHOLE table and all rows 5 whole times. fetch and PDO::FETCH_ASSOC are a bit unclear to me, and I am understanding the $row as either true or false.

$stmt= $pdo->query("SELECT make, model, year, mileage from autos");
$row = $stmt->fetch(PDO::FETCH_ASSOC); 

if (   isset($_SESSION['success']) && $row == true  ) {

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

echo ('<table border="1">'."\n");
    echo "<tr><td>";
    echo "Make";
    echo ("</td><td>");
    echo "Model";
    echo ("</td><td>");
    echo "Year";
    echo ("</td><td>");
    echo "Mileage";
    echo ("</td><td>");
    echo "Action";
    echo ("</td></tr>");
    
    echo "<tr><td>";
    echo (htmlentities($row['make']));
    echo ("</td><td>");
    echo (htmlentities($row['model']));
    echo ("</td><td>");
    echo (htmlentities($row['year']));
    echo ("</td><td>");
    echo (htmlentities($row['mileage']));
    echo ("</td><td>");
    echo ('<a href="edit.php?user_id='.$row['user_id'].'">Edit</a>'.'/'.'<a 
    href="delete".php?user_id='.$row['user_id'].'>Delete</a>');
    echo "</td></tr>";
  }



  } elseif (    isset($_SESSION['success']) && $row == false  ){

  echo "No rows found";
  }

CodePudding user response:

I keep getting the whole table printed, say 5 times.

Because you're printing the whole table on each iteration of the loop:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  echo ('<table border="1">'."\n");
  // etc.

Instead of checking whether the first row is true, check the row count:

$stmt= $pdo->query("SELECT make, model, year, mileage from autos");
$count = $stmt->rowCount();

if (   isset($_SESSION['success']) && $count > 0  )

Then output your table outside the loop and just output each row inside the loop:

echo ('<table border="1">'."\n");
// etc, output the table header
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  echo "<tr><td>";
  // etc.
  echo "</td></tr>";
}
echo ('</table>'."\n");
  • Related