What if it's NULL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

What if it's NULL?

Post by tstimple »

Hello, and thanks again to all who answer these questions from us newbies...

I have the following code:

Code: Select all

<?php
$dlrID = $_GET['dlrID']; 
$connect=mysql_connect('host', 'user', 'pass') or die('Could not connect due to  '.mysql_error()); 
$database=mysql_select_db('database') or die ('Unable to select DB due to  '.mysql_error()); 
$sql = "select NAME, logopath, WebSite, Street, City, Phone1 from Dealerlist where gaID='$dlrID'" or die('Query failed due to  '.mysql_error()); 
$result=mysql_query($sql) or die('Result call failed due to  '.mysql_error()); 
$rows = mysql_fetch_row($result); 
$website = $rows[2]; 
?>
Later in the page I display a link to a website by using:

Code: Select all

&lt;a href="&lt;? echo $website; ?&gt;" target="_blank"&gt;Visit Our Website&lt;/a&gt;
A simple question (I think)...
How would I adjust this code to NOT display a link for the website if the table returned a NULL value (not everyone has a website)

I am learning..... :oops:
Thanks,
--Tim
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Code: Select all

$website = trim($website);
if ( $website <> "" ) {
echo "<a href=".$website."" target="_blank">Visit Our Website</a>!";
}
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

parse error

Post by tstimple »

I now get:

Parse error: parse error, expecting `','' or `';'' in blahblahblah
Do you have an extra quote or something in there???

PS - I agree BUSH, YOU SUCK!
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

He accidently forgot a quote.

Code: Select all

<?php
$website = trim($website); 
if ( $website <> "" ) { 
echo "<a href="".$website."" target="_blank">Visit Our Website</a>!"; 
} 
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

BUSH SUCKS AND BLAIR SUCKS.
If they expect any support next time they decide to do the war thing they will be in for a shock.... god damn any cowboy with his finger on the big red "KILL THEM ALL" button, and god damn any smiley git who kisses that cowboys arse.

Erm, anyway, back to PHP I guess :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Instead of testing for an empty string, you can use the empty() function instead (it can make it easier to remember what you were trying to do with the code later) - it will make sure that the variable has a value which is not 0, an empty string, NULL or false:

Code: Select all

<?php
$website = trim($website);

if (!empty($website)) {
    echo '<a href="'.$website.'" target="_blank">Visit Our Website</a>!';
}
?>
Mac
Post Reply