Jump to content

[SOLVED] How not to display blank data from mysql db


MP145

Recommended Posts

Here is my code.

<?php

//Connects to Database
mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database");
$db = mysql_select_db("$dbname") or die("Unable to select db");

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM members ORDER BY id LIMIT 3") or die(mysql_error());

$num=mysql_numrows($data);

if ($num==0) {
echo "No Data";
} else {
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
  echo ''.$info['street1'].'<br>'.$info['street2'].'<br>'.$info['street3'].'<br>'.$info['pcode'].'<br>'.$info['city'].'<br>'.$info['state'].'<br>';
  }
  }
?>

 

I am trying to list down a person's address. It does the job but when there is no data for the field street2 or street3 it still prints the <br> and there will be a gap between street 1 and the pcode. how can i overcome this ? do i have to use the if else ? or is there any other way?

yea i think you will need else if

while($info = mysql_fetch_array($data)){
    echo $info['street1']."<br>";
    echo $info['street2'] == '' ? '' : $info['street2']."<br>";
    echo $info['street3'] == '' ? '' : $info['street3']."<br>";
    echo $info['pcode'].'<br>'.$info['city'].'<br>'.$info['state'].'<br>';
}

 

Scott.

Thank you very much ratcateme for the reply  ;)

 

and thanks a million for the code, works like a charm with short line, where else i went and did this.

 

<?php
include("ullemasuk.php");
//Connects to Database
mysql_connect($dbhost,$dbuser,$dbpass) or die("Error:: can't connect to database");
$db = mysql_select_db("$dbname") or die("Unable to select db");

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM members ORDER BY id LIMIT 3") or die(mysql_error());

$num=mysql_numrows($data);

if ($num==0) {
echo "No Data";
} else {
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
  echo ''.$info['street1'].'<br>';
  if (!empty($info['street2']))
    {
    echo ''.$info['street2'].'<br>';
    }
    if (!empty($info['street3']))
    {
    echo ''.$info['street3'].'<br>';
    }
    if (!empty($info['pcode']))
    {
    echo ''.$info['pcode'].'<br>';
    }
    if (!empty($info['city']))
    {
    echo ''.$info['city'].'<br>';
    }
    if (!empty($info['state']))
    {
    echo ''.$info['state'].'<br>';
    }
    else {
      echo null;
    }
    echo '<br>';
  }
  }
?>

Works also but, i'll definitely use your code  ;D.

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.