0

I can't see the:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Can anyone help as my code looks spot on:

<?php
//retreive questions from database and put into question box

$query = "SELECT `QuestionId`, `Question`, `Opt1`, `Opt2`, `Opt3`, `Opt4`,`Answer` FROM `pf_questions`";

$question = mysql_query($query);

while($row = mysql_fetch_array($question)){

    $id         = $row['QuestionId'];
    $question   = $row['Question'];
    $opt1       = $row['Opt1'];
    $opt2       = $row['Opt2'];
    $opt3       = $row['Opt3'];
    $opt4       = $row['Opt4'];
    $answer     = $row["Answer"];

?>
<div id="ContainerQuestion">
    <span class="Question">Question <?php echo $id; ?>. <?php echo $question; ?></span>

        <p><input type=radio name='q<?php echo $id; ?>' value="<?php echo $opt1; ?>"> <?php echo $opt1; ?> </p>
        <p><input type=radio name='q<?php echo $id; ?>' value="<?php echo $opt2; ?>"> <?php echo $opt2; ?> </p>
        <p><input type=radio name='q<?php echo $id; ?>' value="<?php echo $opt3; ?>"> <?php echo $opt3; ?> </p>

        <p><input type=radio name='q<?php echo $id; ?>' value="<?php echo $opt4; ?>"> <?php echo $opt4; ?> </p>


</div>
<?php
}

Have tried the mysql_error() and nothing gets outputted so i'm assuming my query is correct?

many thanks

4 Answers 4

2

You don't seem to make the mysql connection anywhere in your code at all. Are you sure there is a valid connection to the database?

Secondly, it would be rather advisable to swap over to PDO which is much safer, shinier and better than the old mysql_* functions.

Having said that, you need to use something like the following to connect using the oder functions:

mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

Edit:

Could you add the following section of code and let me know the output?

$query = "SELECT `QuestionId`, `Question`, `Opt1`, `Opt2`, `Opt3`, `Opt4`,`Answer` FROM `pf_questions`";
$question = mysql_query($query);
$num_rows = mysql_num_rows($question);
echo "$num_rows Rows<br>";
Sign up to request clarification or add additional context in comments.

8 Comments

im just putting it together as quick as with the knowledge I have: not really touched on PDO but know its something I've got to do.
mysql connection happens inside an init.php file in the core folder
Okay, give that a go then, but swapping over to PDO is a really good idea :)
its pulling in the first question and the appropriate $opt variables, just not the rest :(
twenty rows in total and only the first question shows in the while loop - nothing else is shown, no questions and no options
|
0

Sounds like your query is invalid, so the question variable doesn't contain what mysql_fetch_array is waiting for.

Comments

0

It seems that you have something missing in your mysql query, please match the fields (also match for lower case and uppercase) from the actual tables in the DB.

Also sometimes ` is not supported hence remove from all the fields and table name. Probably it will solve your issue.

2 Comments

what do you mean by "its pulling in the first question and the appropriate $opt variables "...is that it's displaying only one equestion and it'soptions?
kindly make your div id dynamic '<div id="ContainerQuestion">' it should be id="ContainerQuestion1" then id="ContainerQuestion2" then id="ContainerQuestion3"...so on...
0

Don`t use mysql_* functions.

$question = mysql_query($query);

What happens if query is failed? Or if mysql server return 0 rows? From manual: "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."

if ( !$question ) {
    //Query is failed. 
    echo mysql_error(), $query; //for developer use. if error happens - you will see what happens
    exit(); //or something else you want
}

if ($question && mysql_num_rows($question) > 0) {
    while($row = mysql_fetch_array($question)){
       .....
    }
} else {
    // mysql server return 0 rows
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.