Jump to content

Help with if else kindly.


takn25

Recommended Posts

Hi here is my statement

 

<?php

$qid=$_GET['qid'];
require_once('database_connect.php');
$answer = "SELECT answers.* FROM answers WHERE aid ='$qid'";	

$resulta=mysql_query($answer) or die (mysql_error()); 
while ($rowa=mysql_fetch_assoc($resulta)) {
	$aid=$rowa['aid'];
	$answer=$rowa['answer'];


      $info_rows = mysql_num_rows($resulta);
  if($info_rows > 0) {echo "$answer";}else {echo "be the first to answer this question";} 
  
  ;}

        ?>

 

 

the IF part is working fine but the else isnt displaying anything at all. Could you kindly tell me, what am i doing wrong and lastly this statement is in a while loop.

Link to comment
https://forums.phpfreaks.com/topic/226118-help-with-if-else-kindly/
Share on other sites

<?PHP
  if(isSet($_GET['qid'])) {
    require_once('database_connent.php');

    $qid = intval($_GET['qid']);
    $query = "SELECT answers.* FROM answers WHERE aid = $qid LIMIT 1";

    if($doQuery = mysql_query($query)) { // Make sure the query executes

      if($result = mysql_num_rows($doQuery)) { // Check for returned rows, if exists echo Answer.
        $aid    = $result['aid'];
        $answer = $result['answer'];

        echo $answer;
        
      } else {
        echo 'Be the first to answer this question.'; // No rows exist, so question has not been answered
      }

    } else {
      echo 'This query failed [ '.$query.' ]'; // Query has failed, echo it out to see why
    }

  } else {
    echo 'There is no question id set.'; // No qid was set from the URL GET parameter
  } 
?>

 

Try using the code above, it's better formatted and includes some minimal validation and error reporting.

 

Regards, PaulRyan.

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.