Select from Database Randomly (Random Query) using PHP/MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 341 times.

In my previous tutorial, I have discussed on How to Limit Query in MySQL which is significant in this tutorial that tackles on how to randomly select from MySQL Table since where going to limit our random result. This is most appropriate in programs like generating random quizzes.

Note: Bootstrap CSS and script used in this tutorial are hosted, therefore, you need internet connection for them to work.

Creating our Database

First step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as testing.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE TABLE `tutorial` (
  2. `tutorialid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `title` VARCHAR(150) NOT NULL,
  4. `link` VARCHAR(150) NOT NULL,
  5. PRIMARY KEY(`tutorialid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  1. INSERT INTO `tutorial` (`tutorialid`, `title`, `link`) VALUES
  2. (1, 'Submit Form using AJAX in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-ajax-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-aja...</a>),
  3. (2, 'Image Upload using AJAX in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-ajax-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-aj...</a>),
  4. (3, 'Creating an Iterator FOR While Loop Forms IN PHP/MySQLi', '<a href="https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-forms-phpmysqli.html'">https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-fo...</a>),
  5. (4, 'Drag, Drop and Insert into Database using AJAX/jQuery in PHP', '<a href="https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-database-using-ajaxjquery-php.html'">https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-...</a>),
  6. (5, 'Simple POS and Inventory System', '<a href="https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system.html'">https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system...</a>),
  7. (6, 'Performance Indicator System', '<a href="https://www.sourcecodester.com/php/11638/performance-indicator-system.html'">https://www.sourcecodester.com/php/11638/performance-indicator-system.html'</a>),
  8. (7, 'Simple Chat System', '<a href="https://www.sourcecodester.com/php/11610/simple-chat-system.html'">https://www.sourcecodester.com/php/11610/simple-chat-system.html'</a>),
  9. (8, 'Uploading Multiple Files INTO MySQL DATABASE USING PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-files-mysql-database-using-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-fi...</a>),
  10. (9, 'Deleting Multiple Rows using Checkbox in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-rows-using-checkbox-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-row...</a>),
  11. (10, 'PHP/MySQLi CRUD Operation with Bootstrap/Modal', '<a href="https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootstrapmodal.html'">https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootst...</a>),
  12. (11, 'PHP Passing VALUE TO Modal USING jQuery', '<a href="https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-modal-using-jquery.html'">https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-MOD...</a>),
  13. (12, 'How to Add Class to a Div using JQuery', '<a href="https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-div-using-jquery.html'">https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-...</a>),
  14. (13, 'How to Limit Number of Items per Row in While Loop using PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-items-row-while-loop-using-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-item...</a>),
  15. (14, 'PHP Prevent the RETURN TO Login Page/Disable Back after Login', '<a href="https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-login-pagedisable-back-after-login.html'">https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-lo...</a>);

randomquery

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.

  1. <?php
  2.  
  3. $conn = mysqli_connect("localhost","root","","testing");
  4. if (!$conn) {
  5. die("Connection failed: " . mysqli_connect_error());
  6. }
  7.  
  8. ?>

index.php

This contains our result table. Also I have added addition codes in the comment in case your dealing with large volume of data to randomly select.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Select from Database Randomly (Random Query) using PHP/MySQLi</title>
  5. <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  6. " rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></s...</a> <link rel="stylesheet" href="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></a>
  7. </head>
  8. <body>
  9. <div class="container">
  10. <div class="row" style="margin-top:30px;">
  11. <form method="POST">
  12. <input type="submit" name="refresh" value="Refresh" class="btn btn-primary">
  13. </form>
  14. </div>
  15. <div class="row" style="margin-top:10px;">
  16. <table class="table table-bordered table-striped">
  17. <thead>
  18. <th>ID</th>
  19. <th>Title</th>
  20. <th>Link</th>
  21. </thead>
  22. <tbody>
  23. <?php
  24. //use this if you have small number of data
  25. include('conn.php');
  26. $query=mysqli_query($conn,"select * from tutorial order by rand() limit 10");
  27.  
  28. //for large volume of data
  29. //include('conn.php');
  30. //$q=mysqli_query($conn,"select * from tutorial");
  31. //$num=mysqli_num_rows($q);
  32. //$limit=10; //limit
  33. //$random=$limit/$num;
  34. //$query=mysqli_query($conn,"select * from tutorial order by rand()<=$random limit 10");
  35.  
  36. while($row=mysqli_fetch_array($query)){
  37. ?>
  38. <tr>
  39. <td><?php echo $row['tutorialid']; ?></td>
  40. <td><?php echo $row['title']; ?></td>
  41. <td><?php echo $row['link']; ?></td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. </tbody>
  47. </table>
  48. </div>
  49. </div>
  50. </body>
  51. </html>

That's it guys. If you have any comments or questions, feel free to write it below or message me. Happy Coding :)


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.