Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in (PLEASE HELP ME WITH THIS WARNING I REALLY NEED THIS)


carayang

Recommended Posts


<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
}
?>
<form method="POST" action="hist1.php">
<br />
<input type="hidden" name="name" value="<?php echo $name ?>" />
<?php
$q = mysql_query("SELECT * FROM histact1 ORDER BY RAND() LIMIT 1");
while ($r1 = mysql_fetch_array($q)){
$id = $r1[0];
$question1 = $r1[1];
$opt1 = $r1[3];
$opt2 = $r1[4];
$opt3 = $r1[5];
?>


<div class="Qset" id="q1"><br /><br />
<label class="items">1st Question :</label> <br />
<center>
<textarea class="textareaQ" name="question1" readonly><?php echo $question1; ?></textarea>
</center>
<br /><br />
<p class="marA">
<input type="radio" name="rad1" value="<?php echo $opt1; ?>" />
<label class="lbl"><?php echo $opt1 ?></label><br />


<input type="radio" name="rad1" value="<?php echo $opt2; ?>" />
<label class="lbl"><?php echo $opt2 ?></label><br />


<input type="radio" name="rad1" value="<?php echo $opt3; ?>" />
<label class="lbl"><?php echo $opt3 ?></label><br />
</p>
</div>
<div class="lr">
<center>
<br /><br /><br /><br />
<a class="nxt"  href="#q2"><label title="Proceed to 2nd Question">Next</label></a>
</center>
</div>
<br /><br /><br />
<center><hr width="90%" /></center><br />
<?php } ?>


<br /><br />
<?php
$q = mysql_query("SELECT * FROM histact1 ORDER BY RAND() LIMIT 1");
while ($r1 = mysql_fetch_array($q)){
$id = $r1[0];
$question2 = $r1[1];
$opt1 = $r1[3];
$opt2 = $r1[4];
$opt3 = $r1[5];
?>


<div class="Qset" id="q2"><br /><br />
<label class="items">2nd Question :</label> <br />
<center>
<textarea class="textareaQ" name="q2" readonly><?php echo $question2; ?></textarea>
</center>
<br /><br />
<p class="marA">
<input type="radio" name="rad2" value="<?php echo $opt1; ?>" />
<label class="lbl"><?php echo $opt1 ?></label><br />


<input type="radio" name="rad2" value="<?php echo $opt2; ?>" />
<label class="lbl"><?php echo $opt2 ?></label><br />


<input type="radio" name="rad2" value="<?php echo $opt3; ?>" />
<label class="lbl"><?php echo $opt3 ?></label><br />
</p>
</div>
<div class="lr">
<center>
<br /><br /><br /><br />
<a class="nxt"  href="#q1"><label title="Proceed to 1st Question">Back</label></a>  |  
<a class="nxt"  href="#q3"><label title="Proceed to 3rd Question">Next</label></a>
</center>
</div>
<br /><br /><br />
<center><hr width="90%" /></center><br />
<?php } ?>


<br /><br />
<?php
$q = mysql_query("SELECT * FROM histact1 ORDER BY RAND() LIMIT 1");
while ($r1 = mysql_fetch_array($q)){
$id = $r1[0];
$question3 = $r1[1];
$opt1 = $r1[3];
$opt2 = $r1[4];
$opt3 = $r1[5];
?>


<div class="Qset" id="q3"><br /><br />
<label class="items">3rd Question :</label> <br />
<center>
<textarea class="textareaQ" name="q3" readonly><?php echo $question3; ?></textarea>
</center>
<br /><br />
<p class="marA">
<input type="radio" name="rad3" value="<?php echo $opt1; ?>" />
<label class="lbl"><?php echo $opt1 ?></label><br />


<input type="radio" name="rad3" value="<?php echo $opt2; ?>" />
<label class="lbl"><?php echo $opt2 ?></label><br />


<input type="radio" name="rad3" value="<?php echo $opt3; ?>" />
<label class="lbl"><?php echo $opt3 ?></label><br />
</p>
</div>
<div class="lr">
<center>
<br /><br /><br /><br />
<a class="nxt"  href="#q2"><label title="Proceed to 2nd Question">Back</label></a>  |  
<a class="nxt"  href="#q4"><label title="Proceed to 4th Question">Next</label></a>
</center>
</div>
<br /><br /><br />
<center><hr width="90%" /></center><br />
<?php } ?>


<br /><br />
<?php
$q = mysql_query("SELECT * FROM histact1 ORDER BY RAND() LIMIT 1");
while ($r1 = mysql_fetch_array($q)){
$id = $r1[0];
$question4 = $r1[1];
$opt1 = $r1[3];
$opt2 = $r1[4];
$opt3 = $r1[5];
?>


<div class="Qset" id="q4"><br /><br />
<label class="items">4th Question :</label> <br />
<center>
<textarea class="textareaQ" name="q4" readonly><?php echo $question4; ?></textarea>
</center>
<br /><br />
<p class="marA">
<input type="radio" name="rad4" value="<?php echo $opt1; ?>" />
<label class="lbl"><?php echo $opt1 ?></label><br />


<input type="radio" name="rad4" value="<?php echo $opt2; ?>" />
<label class="lbl"><?php echo $opt2 ?></label><br />


<input type="radio" name="rad4" value="<?php echo $opt3; ?>" />
<label class="lbl"><?php echo $opt3 ?></label><br />
</p>
</div>
<div class="lr">
<center>
<br /><br /><br /><br />
<a class="nxt"  href="#q3"><label title="Proceed to 3rd Question">Back</label></a>  |  
<a class="nxt"  href="#q5"><label title="Proceed to 5th Question">Next</label></a>
</center>
</div>
<br /><br /><br />
<center><hr width="90%" /></center><br />
<?php } ?>


<br /><br />
<?php
$q = mysql_query("SELECT * FROM histact1 WHERE question != '$question1' AND question != '$question2' AND question != '$question3' AND question != '$question4' ORDER BY RAND() LIMIT 1");
while ($r1 = mysql_fetch_array($q)){
$id = $r1[0];
$question5 = $r1[1];
$opt1 = $r1[3];
$opt2 = $r1[4];
$opt3 = $r1[5];
?>


<div class="Qset" id="q5"><br /><br />
<label class="items">5th Question :</label> <br />
<center>
<textarea class="textareaQ" name="q5" readonly><?php echo $question5; ?></textarea>
</center>
<br /><br />
<p class="marA">
<input type="radio" name="rad5" value="<?php echo $opt1; ?>" />
<label class="lbl"><?php echo $opt1 ?></label><br />


<input type="radio" name="rad5" value="<?php echo $opt2; ?>" />
<label class="lbl"><?php echo $opt2 ?></label><br />


<input type="radio" name="rad5" value="<?php echo $opt3; ?>" />
<label class="lbl"><?php echo $opt3 ?></label><br />
</p>
</div>
<div class="lr">
<center>
<br /><br /><br /><br />
<a class="nxt"  href="#q4"><label title="Proceed to 4th Question">Back</label></a>  |  
<input type="submit" title="Submit Answers" name="submit" class="submit" value=" Submit " onclick="return confirm('Are you sure you want to submit your answers?\nYou can review your answer by click the Back link')" />
</center>
</div>
<br /><br /><br />
<center><hr width="90%" /></center><br />
<?php } ?>
</form>
Edited by mac_gyver
code in code tags please
Link to comment
Share on other sites

that error is a very common one. if you do a search for it, you will both find what it means and how to find what is causing it.

 

next, if you find yourself repeating code over and over that only differs in the data or value it uses, you are doing things the hard way. you should run ONE query that retrieves the data you want in the order that you want it (even if that is a random order), then loop over the rows that the query returns.

Link to comment
Share on other sites

I don't have to look into your code, this question is asked so many times!

 

mysql_query gives a resource on succes but FALSE if an error occurs.

 

then on the next rule you get an error because you did not deliver an resource to the function mysql_fetch_array()

 

 

Some other tips:

- Use mysqli or PDO

- Use mysql(i)_fetch_assoc and then you could get your data like this: echo $r1['columnname']

  That will make your code much more readable!

- Keep your php logic (where you run your queries etc) seperated from your output.

  Just start with <?php ......... ?> and then at the bottom of the page comes your HTML inwhere you use snippets of PHP code just to output your variables

Edited by Frank_b
Link to comment
Share on other sites


<?php

// Here we will prepare the page. We do NOT output anything.
// That means that we do not use echo OR printf functions!

// imagine this comes out of the database:
$myname = 'Frank';

?>
<!------ HERE STARTS THE OUTPUT -------->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Learning Document</title>
</head>

<body>
<h1>Hello <?php echo $myname; ?></h1>
</body>
</html>

 

Edited by Frank_b
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.