Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Generate a random record

    Hi

    I have a table in a DB with various people's names in it... I was wondering if there is some script that I could use to randomly choose a name from that table...

    This is the shell that will be holding this code:

    Code:
    		echo "<table cellpadding=1 border=1 width=40%>"; 	 
    			echo "<tr>";
    			echo "<td>Random Name</td>";
    			echo "</tr>";
    			echo "<tr>";
    			echo "<td>$row[name]  </td>";
    			echo "</tr>";
    			echo "</table>";
    Right now the line in red above pulls out the name from the DB based on the $_GET['id'] but ideally I would like this to be a randomly chosen name from the DB...

    Thanks for your help!

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    SELECT * FROM table ORDER BY rand() LIMIT 1;

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Gjay... so I alreay have this "SELECT" quiery in my script:

    Code:
    SELECT name FROM product WHERE id='$id'
    But also on this page I want to generate the random name...

    So would I add anothe SELECT quiery or modify this one?

    Thanks

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    If you want to display a random one as well as the specific one, you'd need a new query

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok... So for the new quiery do I have to close out the PHP and then start a new one or can you have you "SELECT" quieries within one script?

  • #6
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    echo rand($row[name]);

    probably?

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your feedback... actually i had to create a whole new query... ended up doing this"

    Code:
    // create random query 
    $rand = "SELECT name FROM product ORDER BY RAND() LIMIT 2"; 
    
    // execute query 
    $result = mysql_query($rand) or die ("Error in query: ".$rand."<br/>".mysql_error());   
    $row1 = mysql_fetch_array($result); 
    $row2 = mysql_fetch_array($result); 
    
            echo "<table cellpadding=1 border=1 width=100%>";      
                echo "<tr>"; 
                echo "<td>Random Name:</td>"; 
                echo "</tr>"; 
                echo "<tr>"; 
                echo "<td>$row1[name]</td>"; 
                echo "</tr>"; 
                echo "<tr>"; 
                echo "<td>$row2[name]</td>"; 
                echo "</tr>"; 
                echo "</table>";
    Found this in an example in a PHP for dummies book I have... It's just so much easier to reference the internet than a book... but sometimes... a little reading doesn't hurt

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by losse
    Thanks for your feedback... actually i had to create a whole new query... ended up doing this"

    Code:
    // create random query 
    $rand = "SELECT name FROM product ORDER BY RAND() LIMIT 2"; 
    
    // execute query 
    $result = mysql_query($rand) or die ("Error in query: ".$rand."<br/>".mysql_error());   
    $row1 = mysql_fetch_array($result); 
    $row2 = mysql_fetch_array($result); 
    
            echo "<table cellpadding=1 border=1 width=100%>";      
                echo "<tr>"; 
                echo "<td>Random Name:</td>"; 
                echo "</tr>"; 
                echo "<tr>"; 
                echo "<td>$row1[name]</td>"; 
                echo "</tr>"; 
                echo "<tr>"; 
                echo "<td>$row2[name]</td>"; 
                echo "</tr>"; 
                echo "</table>";
    Found this in an example in a PHP for dummies book I have... It's just so much easier to reference the internet than a book... but sometimes... a little reading doesn't hurt
    Oh, so I'm a PHP book for a dummy now am I?

    You don't think I stick to a single forum do you?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •