carayang Posted October 9, 2014 Share Posted October 9, 2014 (edited) <?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 October 9, 2014 by mac_gyver code in code tags please Quote Link to comment https://forums.phpfreaks.com/topic/291533-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in-please-help-me-with-this-warning-i-really-need-this/ Share on other sites More sharing options...
Ch0cu3r Posted October 9, 2014 Share Posted October 9, 2014 This error usually indicates there is a problem with the query. Use mysql_error to retrieve the error message from MySQL. NOTE: Please stop using the mysql_* functions they are deprecated and no longer supported. You should instead use either MySQLi or PDO Quote Link to comment https://forums.phpfreaks.com/topic/291533-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in-please-help-me-with-this-warning-i-really-need-this/#findComment-1493143 Share on other sites More sharing options...
mac_gyver Posted October 9, 2014 Share Posted October 9, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291533-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in-please-help-me-with-this-warning-i-really-need-this/#findComment-1493144 Share on other sites More sharing options...
Frank_b Posted October 9, 2014 Share Posted October 9, 2014 (edited) 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 October 9, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291533-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in-please-help-me-with-this-warning-i-really-need-this/#findComment-1493146 Share on other sites More sharing options...
Frank_b Posted October 9, 2014 Share Posted October 9, 2014 (edited) <?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 October 9, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291533-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean-given-in-please-help-me-with-this-warning-i-really-need-this/#findComment-1493147 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.