php mysql querys

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
bazconnolly
Forum Newbie
Posts: 1
Joined: Thu Oct 20, 2011 3:17 pm

php mysql querys

Post by bazconnolly »

Hi All

I am a bit stuck with what is probably very simple.

I have a script to query a mysql property database, I need to know how to use conditions within the record set. Here is my code:

Code: Select all

  <?php 
 


$query = ("SELECT * FROM property ORDER BY RAND() LIMIT 10")
or die(mysql_error());

$result = mysql_query($query,$conn);

while($row = mysql_fetch_array($result)) {
?> 

<div class="res-holder">

<div class="res-img-border">
<div class="res-img"><a href="/propertydetails.php?PID=<?php echo$row['PID'];?>"><img src="<?php echo $row['MainImg'] ; ?>" width="130" height="90" border="0"/></a></div>
</div>
<div class="res-price">£ <?php echo $row['Price']; ?></div>

<div class="res-type"><?php echo $row['Beds']; ?> Bed <?php echo $row['Type']; ?></div>

<div class="res-loc"><?php echo $row['L3Name']; ?></div>



</div><!-- res-holder END -->

<?php } ?>
What I need to do is when the a record is returned that has a null value for the 'L3Name field' . then the script should show the L2Name field instead for example:

Code: Select all

<div class="res-loc"> if  { $row['L3Name'] = null }  echo $row['L2Name'] else { echo ['L3Name'] } </div>
. I know this code is wrong but can anyone show me how to right this line of code,

Any help would be very much appreciated

thanks

Baz
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php mysql querys

Post by Celauran »

You're using the assignment operator (=) instead of the comparison operator(==).

Code: Select all

echo (!empty($row['L3Name'])) ? $row['L3Name'] : $row['L2Name'];
Post Reply