Select from Database Randomly (Random Query) using PHP/MySQLi
Submitted by nurhodelta_17 on Wednesday, October 11, 2017 - 13:10.
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.
That's it guys. If you have any comments or questions, feel free to write it below or message me. Happy Coding :)
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.- CREATE TABLE `tutorial` (
- `tutorialid` INT(11) NOT NULL AUTO_INCREMENT,
- `title` VARCHAR(150) NOT NULL,
- `link` VARCHAR(150) NOT NULL,
- PRIMARY KEY(`tutorialid`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- INSERT INTO `tutorial` (`tutorialid`, `title`, `link`) VALUES
- (1, 'Submit Form using AJAX in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-ajax-phpmysqli.html'),
- (2, 'Image Upload using AJAX in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-ajax-phpmysqli.html'),
- (3, 'Creating an Iterator for While Loop Forms in PHP/MySQLi', 'https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-forms-phpmysqli.html'),
- (4, 'Drag, Drop and Insert into Database using AJAX/jQuery in PHP', 'https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-database-using-ajaxjquery-php.html'),
- (5, 'Simple POS and Inventory System', 'https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system.html'),
- (6, 'Performance Indicator System', 'https://www.sourcecodester.com/php/11638/performance-indicator-system.html'),
- (7, 'Simple Chat System', 'https://www.sourcecodester.com/php/11610/simple-chat-system.html'),
- (8, 'Uploading Multiple Files into MySQL Database using PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-files-mysql-database-using-phpmysqli.html'),
- (9, 'Deleting Multiple Rows using Checkbox in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-rows-using-checkbox-phpmysqli.html'),
- (10, 'PHP/MySQLi CRUD Operation with Bootstrap/Modal', 'https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootstrapmodal.html'),
- (11, 'PHP Passing Value to Modal using jQuery', 'https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-modal-using-jquery.html'),
- (12, 'How to Add Class to a Div using JQuery', 'https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-div-using-jquery.html'),
- (13, 'How to Limit Number of Items per Row in While Loop using PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-items-row-while-loop-using-phpmysqli.html'),
- (14, 'PHP Prevent the Return to Login Page/Disable Back after Login', 'https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-login-pagedisable-back-after-login.html');
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.- <?php
- if (!$conn) {
- }
- ?>
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.- <!DOCTYPE html>
- <html>
- <head>
- <title>Select from Database Randomly (Random Query) using PHP/MySQLi</title>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <div class="row" style="margin-top:30px;">
- <form method="POST">
- <input type="submit" name="refresh" value="Refresh" class="btn btn-primary">
- </form>
- </div>
- <div class="row" style="margin-top:10px;">
- <table class="table table-bordered table-striped">
- <thead>
- <th>ID</th>
- <th>Title</th>
- <th>Link</th>
- </thead>
- <tbody>
- <?php
- //use this if you have small number of data
- include('conn.php');
- //for large volume of data
- //include('conn.php');
- //$q=mysqli_query($conn,"select * from tutorial");
- //$num=mysqli_num_rows($q);
- //$limit=10; //limit
- //$random=$limit/$num;
- //$query=mysqli_query($conn,"select * from tutorial order by rand()<=$random limit 10");
- ?>
- <tr>
- <td><?php echo $row['tutorialid']; ?></td>
- <td><?php echo $row['title']; ?></td>
- <td><?php echo $row['link']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Comments
Add new comment
- Add new comment
- 647 views