numtre Posted November 9, 2008 Share Posted November 9, 2008 Hello everyone Here is my code <?php $con = mysql_connect("localhost","inteoria_crash","-----"); if (!$con) { die ('Could not connect.'.mysql_error()); } mysql_select_db ("inteoria_vera",$con) or die(mysql_error()); $result=mysql_query("SELECT news_ID, DATE_FORMAT(date, '%d/%m/%Y') as formatted_date, content FROM tblNews ORDER BY date DESC LIMIT 0, 3 "); $row=mysql_fetch_array($result); $num=mysql_numrows($result); mysql_close($con); $i=0; while ($i < $num) { $date=mysql_result($result,$i,"formatted_date"); $content=mysql_result($result,$i,"content"); echo "<span class='bold'>$date</span>: $content<br>"; $i++; } ?> What I want is to display some text (like "sorry no news") in case there are no news posted at the moment. I know i need an if/else loop, what i'm missing is how to tell php if $result is empty then whatever... can you help me? I have been looking on the net but no joy yet, although i know it must be super easy thanks a lot guys Alex Quote Link to comment Share on other sites More sharing options...
trq Posted November 9, 2008 Share Posted November 9, 2008 <?php $con = mysql_connect("localhost","inteoria_crash","-----") or die ('Could not connect.'.mysql_error()); mysql_select_db ("inteoria_vera",$con) or die(mysql_error()); $sql = "SELECT news_ID, DATE_FORMAT(date, '%d/%m/%Y') as formatted_date, content FROM tblNews ORDER BY date DESC LIMIT 0, 3"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { echo "<span class='bold'>{$row['formatted_date']}</span>: {$row['content']}<br>"; } } else { echo "Sorry, no news"; } } ?> Quote Link to comment Share on other sites More sharing options...
numtre Posted November 9, 2008 Author Share Posted November 9, 2008 this is great thanks man! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.