retrieving msql database records

Discussion in 'PHP' started by jjh, Mar 15, 2007.

  1. #1
    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>
     
    jjh, Mar 15, 2007 IP
  2. php_daemon

    php_daemon Active Member

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #2
    
    $result = mysql_query("SELECT * FROM table1 ORDER BY RAND() LIMIT 1")
    or die(mysql_error()); 
    
    PHP:
     
    php_daemon, Mar 15, 2007 IP
  3. petko

    petko Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi jjh,

    Some assistance for you:
    http://www.onlinehowto.net/Tutorials/Development/WEB/PHP/44
     
    petko, Mar 16, 2007 IP
  4. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    SELECT * FROM table1 ORDER BY RAND() LIMIT 0,1
     
    jitesh, Mar 16, 2007 IP
  5. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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:
     
    mikemotorcade, Mar 16, 2007 IP