0

I'm making a website for myself to improve solving math equations. I inserted some equations to my database and now the website loads one random equation at a time.

<?php
$con=mysqli_connect("localhost", "", "", "math");
$rows=mysqli_query($con, "SELECT * FROM equations");
$rows = mysqli_num_rows($rows);
$num = rand(1, $rows);
$sql = "SELECT * FROM equations WHERE id='$num'";
$result = mysqli_query($con, $sql);
$datas = array();

if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $datas[] = $row;
        }
    }
    foreach ($datas as $data) {
        echo $data['equation'];
    }
?>

The problem is that as I'm putting new equations into the database it loads slower and slower. Is there anything I can do to make it load faster?

bmt24
  • 259
  • 1
  • 3
  • 6
  • Can you tell us something about the `id` values? What do they look like? – Tim Biegeleisen Oct 23 '18 at 03:25
  • The `id` values are just ids of the equations. It chooses a random number, depending on how many equations are there (how many rows are in the database) and then displays the equation that has the id of the random number. The equations have ids from 1 (another has 2, 3, 4 etc.) – bmt24 Oct 23 '18 at 03:56
  • If you know what the range of `id` values are, you may add an index on the `id` column if it does not already have one. Then, those selects should be reasonably fast, even for large tables. – Tim Biegeleisen Oct 23 '18 at 04:02

0 Answers0