0

I'm having a problem in this simple SQL/PHP query...

<?php
$course=$row['course'];
include('../db.php');
$cat=$row['cat'];
$result = mysql_query("SELECT * FROM question WHERE course='$course' AND cat='$cat'");
while($row = mysql_fetch_array($result))
    {
        echo $row['question'].'?<br>';
        $qid=$row['qid'];
        echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
        echo '<select name="answer[]">';
        echo '<option>Select Answer></option>';
        $resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 4");
            while($rowik = mysql_fetch_array($resultik))
                {
                echo '<option>';
                echo $rowik['opt'];
                echo '</option>';
                }
        echo '</select><br><br>';
    }
?>

Basically, this is a online examination. I want to display all the questions if the student will login. And the questions will be order/arrange according by their course. But eventually, there's no display at all. Not even a single letter will display.

Any help would be appreciated. Thank you so much.

  • Where is `$row` defined for `$course=$row['course']; include('../db.php'); $cat=$row['cat'];`? – Frank Apr 15 '15 at 11:28
  • A blank screen with PHP usually means a syntax error in your code. Your server error log will show you where. –  Apr 15 '15 at 11:29
  • Hello Frank, the db.php include the connection of the database.. – user3324056 Apr 15 '15 at 11:30
  • Hello Hobo, nothing display after executing my code. I don't know what really is wrong.. – user3324056 Apr 15 '15 at 11:31
  • Deprecated mysql. Please read the following for your own benefit. http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli – Derple Apr 15 '15 at 11:36
  • That error (in the answer below which should be deleted) means your `$row` is undefined or at least the index you're trying to access, `cat` and `course`. – chris85 Apr 15 '15 at 11:36
  • It would be useful to post the error you got here as well. http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – chris85 Apr 15 '15 at 11:37
  • $row should be define above all or if it is defined in anyother file it should be included above – Imran Qamer Apr 15 '15 at 11:38
  • I don't have any textfields in this.. I just want to display all the questions according to the course of the student if they login.. I just run the SQL query that Imran gave, it says that Undefined Variable cat and course.. – user3324056 Apr 15 '15 at 11:40

1 Answers1

1

In this there must some POST or GET values to get the course and cat which means

$course=$row['course'];
$cat=$row['cat'];

Since the $row is empty this is the case it will not display anything. Check with isset() like following

$course = isset($row['course']) ?  $row['course'] : 'COURSE';
$cat    = isset($row['cat']) ? $row['cat'] : 'CAT';

The included file include('../db.php'); please check the database connectivity has established or not?.

Jagadeesh
  • 734
  • 6
  • 26
  • Thanks Jagadessh, It displays now the content but not in order.. I'm using WHERE condition in mysqli, is WHERE condition id deprecated in mysqli? Is there any other way? this is my sql code `code`SELECT * FROM question WHERE course='$course' `code` but it's not showing anything. But if I remove the where condition it shows all the content.. – user3324056 Apr 15 '15 at 12:37
  • try to echo your value **$course** or your query and check with your database directly. Another way set the default course example `code` SELECT * FROM question WHERE course='php' `code` If it works then please check with the values of $course. – Jagadeesh Apr 15 '15 at 12:44
  • It already show up now Jagadeesh. Thanks for that. One more thing, how can I display it according to the subject. I'm using accordion style. And this is now it looks like [link]http://prntscr.com/6u0m1d[link] . I have 4 courses and same question every course, but It displays it all in one accordion.. I want them to order or arranged by subject. – user3324056 Apr 15 '15 at 12:49
  • you can use `code` ORDER BY subject `code` in your query so that it can order based on the subject, Results will display in the accordion. am not sure. please try. – Jagadeesh Apr 15 '15 at 13:03
  • The subjects is on the other table, and I need to create a inner join query where I can arranged them by subject. Here's my SQL Query, `code` SELECT * FROM question INNER JOIN subject ON subject = cat WHERE course='$course' AND cat='$cat' ORDER BY `subject` `code` I have question and subject tables in my database. – user3324056 Apr 15 '15 at 15:45