user1611341
user1611341

Reputation: 35

php while loop error using mysql_fetch_array

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

Upvotes: 0

Views: 591

Answers (4)

Fluffeh
Fluffeh

Reputation: 33512

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>";

Upvotes: 2

AlexeyKa
AlexeyKa

Reputation: 558

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
}

Upvotes: 0

Anil agrahari
Anil agrahari

Reputation: 340

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.

Upvotes: 0

Aerilys
Aerilys

Reputation: 1639

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

Upvotes: 0

Related Questions