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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.