Page 1 of 1

What if it's NULL?

Posted: Sat Jan 24, 2004 10:11 pm
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

Posted: Sat Jan 24, 2004 10:16 pm
by Straterra

Code: Select all

$website = trim($website);
if ( $website <> "" ) {
echo "<a href=".$website."" target="_blank">Visit Our Website</a>!";
}

parse error

Posted: Sat Jan 24, 2004 10:39 pm
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!

Posted: Sat Jan 24, 2004 11:09 pm
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>!"; 
} 
?>

Posted: Sun Jan 25, 2004 12:42 am
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 :)

Posted: Mon Jan 26, 2004 5:00 am
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