How to make $ row not output if there is no record in the database

<?php require 'scripts/connect.php'; $sqlAdd = (!empty($_GET["sort"]) && $_GET["sort"]=='name') ? " Order by name" : ""; $sql_select = "SELECT * FROM users".$sqlAdd; $result = mysql_query($sql_select); $row = mysql_fetch_array($result); do { printf("<tr> <td>" .$row['name'] . "</td> <td>" .$row['last_name'] ."</td> <td><img src=/db1/scripts/upload/" .$row['img'] . " style=\"width: 70px;height: 50px;\"></td> <td>" .$row['email'] . "</td> <td>" .$row['facebook'] . "</td> <td>" .$row['time'] . "</td> <td>" .$row['spec'] . "</td> </tr>"); } while($row = mysql_fetch_array($result)); ?> 
  • do {} while () -> while () {} not? - Dmitriy Simushev

1 answer 1

Replace the loop with a postcondition do..while() with a loop with a precondition while() {}

 $result = mysql_query($sql_select); while ($row = mysql_fetch_array($result)) { printf( ........ ); } 
  • All also, all jsfiddle.net/etr0ddcg columns are displayed . More truly how to add a condition to $ row [img]. If you do not display empty field - Vitaly Nosikov
  • @ VitaliyNosikov What are the columns? The initial question was "How to make $ row not output if there is no record in the database" I showed how. What columns are you talking about? - Anton Shchyrov
  • prnt.sc/d4sexd . In the first line there is no data in 3 columns, but it is displayed. In the second line there is data in the 3rd column. It is displayed as it should - Vitaly Nosikov
  • @ VitaliyNosikov use if or ternary operator together with isset() or empty() functions - Anton Shchyrov
  • Yes, I vkurse if. But I don `t know how to attach to my array - Vitaly Nosikov