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 Link to comment https://forums.phpfreaks.com/topic/132059-easy-i-think-no-result-from-query/ 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/132059-easy-i-think-no-result-from-query/#findComment-686235 Share on other sites More sharing options...
numtre Posted November 9, 2008 Author Share Posted November 9, 2008 this is great thanks man! Link to comment https://forums.phpfreaks.com/topic/132059-easy-i-think-no-result-from-query/#findComment-686243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.