Link to home
Create AccountLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

Taking random record from MYSQL DB using PHP

Hi all,

I have a webpage setup that when the user clicks the submit button a pop-window appears and I want the text in the window to be selected at random from a field in the database.

Could someone give me an example of how to do this?

I have the database setup and connecting correctly. The name of the table where I want to take the text from is called gen_enquiry_table and the field is called gen_enq_txt
 // Connects to your Database 

 mysql_connect("", "bubblesdb", "xxx") or die(mysql_error()); 

 mysql_select_db("bubblesdb") or die(mysql_error()); 

<!DOCTYPE html>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="" type="text/javascript"></script>
<script type="text/javascript">
  function modalPopup() {    
    $("#screen").css("display", "block");
    $("#result").css("display", "block");
  function hide() {
    $("#screen").css("display", "none");
    $("#result").css("display", "none");
<style type="text/css">
.screen {
        background-color: #333; 
        -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
        filter: alpha(opacity=50); 
        opacity: 0.5;
    .result {
        position: absolute;
        top: 100px;
        left: 125px;
        width: 140px;
        height: 90px; 
.style1 {font-size: 36px}
    <p><span class="style1">General Enquiry Page</span> <br>
      <img src="Pictures/question.jpg" width="352" height="438"></p>
    <p><input type="button" value="Get an Enquiry Question " onClick="javascript:modalPopup()" />
    <div id="screen" class="screen"></div>
    <div id="result" class="result">
            <a href="#" onClick="javascript:hide()" title="Hide Preview">Close</a>

Open in new window


Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

@Derek is this what you mean or do you need the entire query written?

SELECT gen_enq_txt FROM gen_enquiry_table ORDER BY RAND() LIMIT 1

Open in new window

Avatar of TLN_CANADA


Hi rgranlund,

If you could write out the full query that would be greatly appreciated and how to display it on the pop up window.

Many thanks,

Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you rgranlund,

Could you tell me where to insert the <?php echo $quote; ?> so that it displays on the pop-up window?
<div id="screen" class="screen"></div>

Open in new window

Should be:

<div id="screen" class="screen"><?php echo $quote; ?> </div>

Open in new window

Thank you! I put it after the other div tag and it's working now. Would it be easy to change the script so if I close the pop-up window and press the submit button for the second time, it takes a different record from the database?
No, you would need to adjust the code a little bit.
Okay, thanks I will play with it and open a new question if necessary.
if (isset "") going off of the submit button?
I'm not sure I understand what you're asking?