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

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.