I have created a database in mysql and inserted records into it. I now want to output these one at a time as a button is clicked. Currently all I can do is get the first out then get this gain when I press the button. Does anyone know how I can generate a random record every time the button is clicked? Thanks <?php mysql_connect("localhost", "hjd", "fggdf") or die(mysql_error()); mysql_select_db("gakhal") or die(mysql_error()); $result = mysql_query("SELECT * FROM table1") or die(mysql_error()); $row = mysql_fetch_array( $result ); print "ID:<br>".$row['PRIMARY KEY']; print "Question:<br>".$row['question']; print "Option A:<br> ".$row['optiona']; print "Option B:<br> ".$row['optionb']; print "Option C:<br> ".$row['optionc']; print "Option D:<br> ".$row['optiond']; if (isset($_POST['submit'])) { $result = mysql_query("SELECT * FROM airlaw") or die(mysql_error()); $row = mysql_fetch_array( $result ); ?> <html> <head> <title>Air Law questions</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> <input type="submit" value="submit" name="submit"> </form> </body> </html>
I don't know why you would want to return a random row each time, but if you want to goes through them one at a time: first off I would think you should move the php code to the inside of the html so that it prints in the correct spot. Next, if you add a hidden field that contains a value that increases each time you submit, then you could add thaqt to your query and return the next row in the database: <html> <head> <title>Air Law questions</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> <?php //get the limit field $limit == $_POST['limit']; //if it is blank, set it at 0 if (!isset($limit)) limit = 0; mysql_connect("localhost", "hjd", "fggdf") or die(mysql_error()); mysql_select_db("gakhal") or die(mysql_error()); //return 1 row starting with the value of $limit $result = mysql_query("SELECT * FROM table1 ORDER BY LIMIT ".$limit.", ".$limit+1."") or die(mysql_error()) $row = mysql_fetch_array( $result ); print "ID:<br>".$row['PRIMARY KEY']; print "Question:<br>".$row['question']; print "Option A:<br> ".$row['optiona']; print "Option B:<br> ".$row['optionb']; print "Option C:<br> ".$row['optionc']; print "Option D:<br> ".$row['optiond']; if (isset($_POST['submit'])) { $result = mysql_query("SELECT * FROM airlaw") or die(mysql_error()); $row = mysql_fetch_array( $result ); ?> <input type="hidden" value="limit" name="<?php echo $limit+1; ?>"> <input type="submit" value="submit" name="submit"> </form> </body> </html> PHP: also, why is this code in there? if (isset($_POST['submit'])) { $result = mysql_query("SELECT * FROM airlaw") or die(mysql_error()); $row = mysql_fetch_array( $result ); PHP: