I need to print a line if $tablerows[18] not 0 . How to do it?

 $db = mysql_connect("localhost", "login", "pass"); mysql_select_db("namedb", $db); $sql = mysql_query("SELECT * FROM gb_users", $db); echo("<table border ='2'>"); echo("<tr><td>Значение</td><td>Значение 2</td><td>Значение 3</td><td>Значение 4</td></tr>"); while ($tablerows = mysql_fetch_row($sql)) { echo("<tr></tr><tr><td>$tablerows[1]</td><td>$tablerows[3]</td><td>$tablerows[8]</td><td>$tablerows[18]</td></tr> <tr></tr> "); } echo "</table>"; mysql_close($db); 

    3 answers 3

    In fact, PHP has nothing to do with it. Despite the fact that you can set the condition if ($tablerows[18] !== 0) , it is easier to make the correct selection from the database itself.

    That is, the sample will look something like this:

     SELECT * FROM gb_users WHERE TRIM(myCol) <> '' /* WHERE `myCol` IS NOT NULL AND TRIM(myCol) <> '' */ 

    where myCol is the name of the column that is checked for emptiness.

    Then in the loop no conditions are needed. For in the final sample there will no longer be those unnecessary data lines.

      Here is what it will look like. Checked running

       <?php $db = mysql_connect("localhost","login","pass"); mysql_select_db("namedb" ,$db); $sql = mysql_query("SELECT * FROM gb_users" ,$db); echo ("<table border ='2'>"); echo ("<tr><td>Значение 1</td><td>Значение 2</td><td>Значение 3</td><td>Значение 4</td></tr>"); while ($tablerows = mysql_fetch_row($sql)) { if ($tablerows[18] != 0){ echo("<tr></tr><tr><td>$tablerows[1]</td><td>$tablerows[3]</td><td>$tablerows[8]</td><td>$tablerows[18]</td></tr> <tr></tr> "); } } echo "</table>"; mysql_close($db); ?> 
         $db = mysql_connect("localhost", "login", "pass"); mysql_select_db("namedb", $db); $sql = mysql_query("SELECT * FROM gb_users", $db); echo("<table border ='2'>"); echo("<tr><td>Значение</td></tr>"); $tablerows = mysql_fetch_row($sql); if (isset($tablerows[18]) && $tablerows[18] !== 0) { echo('<tr><td>' . $tablerows[18] . '</td></tr>'); } echo "</table>"; mysql_close($db);