Jump to content

[easy I think] no result from query


numtre

Recommended Posts

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

<?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";
  }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.