David Passmore
David Passmore

Reputation: 6099

MYSQL : If no data

I have the following mysql query:

$products = mysql_query("SELECT * FROM dpweb_products WHERE id='$id'") or die(mysql_error());
$row = mysql_fetch_array($products);

What I would like to know is if this row is empty:

$row['description']

I want it to display "No Product Description Available" but otherwise display the description value from the database.

Could do with pointing in the right direction.

Thanks

Upvotes: 1

Views: 488

Answers (3)

Sparkup
Sparkup

Reputation: 3754

This should do it:

$products = mysql_query("SELECT * FROM dpweb_products WHERE id='$id'");

if($products === false){ // better way to handle querys

    echo 'There was a problem with the query';

} else {

    if(mysql_num_rows($products) == 0) {

        echo 'No Product Description Available';

    }else{

    $row = mysql_fetch_array($products);
    echo $row['description'];

    }
}

die(); will kill the page. You might not want this.

Upvotes: 0

Ariel
Ariel

Reputation: 26753

echo $row['description'] === '' ? 'No Product Description Available' : $row['description'];

Upvotes: 0

Matt Bradley
Matt Bradley

Reputation: 4495

Try this:

if (empty($row['description'])
    echo 'No Product Description Available';
else
    echo $row['description'];

Upvotes: 2

Related Questions