Jump to content

if, else question


ultraloveninja

Recommended Posts

Hi there!

 

So, I remember doing this correctly some time ago, but now I've forgotten how.  I am try in to get it to echo out something if the results from mysql are empty.  I can get it to return the results if there is something in the database, but it's not echoing out the message from the case statement.  I can't remember how to get it to look for "nothing".

 

Here's my php code:

 

<?php
$sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc";		
$result = mysql_query($sql,$conn);

while ($row = mysql_fetch_array($result)){
$petname = $row['petname'];
$desc = $row['description'];
$family = $row['familyname'];

if ($row=="0") {
echo "No pet tributes for the letter.";
    } else {

echo <<<END


<p><strong>$petname</strong><br />
$desc<br />
<em>- $family</em></p>

END;
}
}
?>

 

I am not sure if I have the if, else in the right place either.  Any help is greatly appreciated.  Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/234886-if-else-question/
Share on other sites

i would add this

while ($row = mysql_fetch_array($result)){
$petname = $row['petname'];
$desc = $row['description'];
$family = $row['familyname'];
$num_rows = mysql_num_rows($result);

if ($num_rows=="0") {
echo "No pet tributes for the letter.";
    } else {

echo <<<END

 

Link to comment
https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207046
Share on other sites

Ahhh...ok.

 

 

Yeah, I did have it here originally:

 

$sql = "select * from tbl_tribue where letter = '$_GET[letter]' order by letter asc";		
$result = mysql_query($sql,$conn);

if ($result=="0") {
echo "<p>No pet tributes for the letter.</p>";
    } else {


...

 

but it wasn't working, I thought that I would need it to check against the rows.

 

But now that I am looking at it your suggestion is the missing link.  Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207051
Share on other sites

Why don't you try something like this?

 

<?PHP

  $letter  = mysql_real_escape_string($_GET['letter']);

  $myQuery = "SELECT * FROM tbl_tribue WHERE letter = '$letter' ORDER BY letter ASC";		

  if($doQuery = mysql_query($myQuery)) {
    if(mysql_num_rows($doQuery)) {

      while($row = mysql_fetch_assoc($doQuery)) {
          echo '<p><strong>', $row['petname'] ,'</strong><br />',
             $desc ,'<br />',
             '<em>- ', $family ,' </em></p>';
      }

    } else {
      echo 'No pet tributes for the letter "<strong>', $letter ,'</strong>"';
    }

  } else {
    echo 'This query failed: "', $myQuery ,'"';
  }

?>

 

Tell me if this works, as it's untested.

 

Regards, PaulRyan.

Link to comment
https://forums.phpfreaks.com/topic/234886-if-else-question/#findComment-1207052
Share on other sites

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.