i am getting the list of values from databas table, some values are null, i want to show the values, which are not null. example is as follow,

//sql query 
while ($row =mysql_query($result))
{
$carBrand     =$row['brandName'];
$carMod        = $row['model'];
$carModYear =$row['modYear'];
$regCity        =$row['regCity'];
$demand      =$row['demand'];
//some of them are null, i want to print here only those are not null
}

actully i have no idea how to do it...
please help..

Dani AI

Generated

The original loop in Post #1 uses the query call where a fetch should be used; that will never return row data. and were headed in the right direction (check values before printing), but two important improvements make the result clearer and safer: 1) filter or supply defaults on the SQL side so unwanted rows/columns never arrive, and 2) use a modern DB API and HTML-escape output before rendering.

A compact SQL-side solution removes NULLs or replaces them with defaults (reduces PHP work and bandwidth). Example SQL patterns:

SELECT id, COALESCE(brandName, 'N/A') AS brandName, model FROM cars WHERE brandName IS NOT NULL;

COALESCE provides a fallback value and WHERE ... IS NOT NULL filters rows. MySQL details: and working with NULLs.

When logic belongs in PHP (for flexible per-row display), fetch rows with a modern API and only render non-empty fields. Using PDO or mysqli avoids the deprecated mysql* calls and supports prepared statements for safety. The approach: fetch an associative row, iterate a small whitelist of expected columns, skip values that are NULL/empty, and HTML-escape any output (so markup stays safe). PDO docs and migration notes: PDO basics and [mysql* deprecation](https://www.php.net/manual/en/migration70.deprecated.php).

Quick troubleshooting tips: confirm whether values are NULL or empty strings (use WHERE column IS NULL OR TRIM(column) = '' to find both), trim whitespace before decision, and decide whether the UI should omit columns entirely or show a fixed table with a short placeholder.

Recommended Answers

All 3 Replies

build tables, and

print "<tr>";
echo "<td>". ( empty($row["brandName"]) ? "-" : (htmlspecialchars($row["brandName"])) )."</td>";
echo "<td>". ( empty($row["model"]) ? "-" : (htmlspecialchars($row["model"])) )."</td>";
echo "<td>". ( empty($row["modYear"]) ? "-" : (htmlspecialchars($row["modYear"])) )."</td>";
echo "<td>". ( empty($row["regCity"]) ? "-" : (htmlspecialchars($row["regCity"])) )."</td>";
echo "<td>". ( empty($row["demand"]) ? "-" : (htmlspecialchars($row["demand"])) )."</td>"; 
print "</tr>";

here is the simple solution:

while ($row =mysql_query($result))
{
if(!empty($row['brandName'])
{
$carBrand     =$row['brandName'];
}
if(!empty($row['model'])
{
$carMod        = $row['model'];
}
---
---
---
}

this is just a example. modified your code according to your needs.

while ($row =mysql_query($result)) ????????

<?php
	include("conn_bd.php");
    $sql="select * from table";

    $result_res=mysql_query($sql) or die(mysql_error());
    print "<table border=\"1\">";
    if(mysql_num_rows($result_res)>0)
    {
        while($rows_res=mysql_fetch_assoc($result_res))
        {
            /* here comes the printed values*/
print "<tr>";
echo "<td>". ( empty($rows_res["brandName"]) ? "&nbsp;" : $rows_res["brandName"] )."</td>";
echo "<td>". ( empty($rows_res["model"]) ? "&nbsp;" : $rows_res["model"] )."</td>";
echo "<td>". ( empty($rows_res["modYear"]) ? "&nbsp;" : $rows_res["modYear"] )."</td>";
echo "<td>". ( empty($rows_res["regCity"]) ? "&nbsp;" : $rows_res["regCity"] )."</td>";
echo "<td>". ( empty($rows_res["demand"]) ? "&nbsp;" : $rows_res["demand"] )."</td>";
print "</tr>";
        }
        print "</table>";
    }
    else
    echo "No result in res table ...";

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.