1

Im setting up a website for my home work and I need to display 1 row at random from an sql table in a html table using php. For example, my website is called random data where the website will generate random data for you (except all it is actually doing is displaying data already inserted into a database). So, does anyone know the code required to display 1 row of data from an sql table and display it in a html table using php in each cell of the table to retrieve the data from the database? Here is what I have so far (it works when I don't try to select just one row at random, but when I select all of them, it does work. Pleas Help! :)

<?php
            $username = "root";
            $password = "root";
            $dbname = "fakedeets";

            $conn = new mysqli("localhost", $username, $password, $dbname);
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $sql = "SELECT FLOOR(RAND()*(10-5+1)+5);";
            $result = $conn->query($sql);

            if($row = $result-> fetch_assoc()) {
                    echo "<tr><td>". $row["firstname"]. 
                    "</td><td>". $row["lastname"].
                    "</td><td>". $row["dob"]. 
                    "</td><td>". $row["gender"]. 
                    "</td><td>". $row["emailaddress"]. 
                    "</td><td>". $row["phonenumber"]. 
                    "</td><td>". $row["photo"]. 
                    "</td></tr>";
                echo "</table>";
            }   else {
                        echo "0 results";
            }

            $conn->close();
?>

What I expect to appear is just 1 row of data displayed on a html doc selected at random from an sql database. Instead it just outputs nothin

  • by the way, the `` tag is missing from the beginning of the table in your code
    – potato Jul 30 '19 at 11:59
  • 3
    You are selecting a random number instead of a random row from a table. You need to select from a table and use `ORDER BY RAND() LIMIT 1` to get a random row. Note that this probably does not scale very well. – jeroen Jul 30 '19 at 12:00
  • Thanks for your replies potato and jeroen. potato, the tag is further up in my code (the provided code is just a snippet of what the issue is). @jeroen, I tried to change my code as you told me to however it does not work? Could you please try run the code yourself and see if it works, if not do you have any other suggestions? Thanks a lot, I really appreciate it!
    – RankWaxsy1911 Jul 31 '19 at 03:39
  • here is all of my code if you require it – RankWaxsy1911 Jul 31 '19 at 03:40
  • Fake Deets – RankWaxsy1911 Jul 31 '19 at 03:42
  • Your Fake Deets Are:

    First Name Last Name Date of Birth Gender Gender Email Address Phone Number Photo
    – RankWaxsy1911 Jul 31 '19 at 03:42
  • connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT ORDER BY RAND() LIMIT 1 FROM fakeentries;"; $result = $conn->query($sql); – RankWaxsy1911 Jul 31 '19 at 03:43
  • if($row = $result-> fetch_assoc()) { echo "". $row["firstname"]. "". $row["lastname"]. "". $row["dob"]. "". $row["gender"]. "". $row["emailaddress"]. "". $row["phonenumber"]. "". $row["photo"]. ""; echo ""; } else { echo "0 results"; } $conn->close(); ?> – RankWaxsy1911 Jul 31 '19 at 03:44
  • I had to paste it amongst lots of comments as it had a character limit, just paste it in order (top to bottom) and it will be the exact code of what I have. Thanks again! – RankWaxsy1911 Jul 31 '19 at 03:45

1 Answers1

2

You can just use SELECT {your columns} FROM {table} ORDER BY RAND() LIMIT 1

This selects the columns from the table you need, orders it randomly and then limits the set to just one entry. Replace the 1 with number of rows you need.

Rutger
  • 135
  • 1
  • 7
  • omg, I didn't see this and it works, Thank you so much find my answer below for those who need it in the future. – RankWaxsy1911 Jul 31 '19 at 03:49
  • SELECT * FROM fakeentries ORDER BY RAND() LIMIT 1; – RankWaxsy1911 Jul 31 '19 at 03:50
  • Awesome that it solved your issue. It will be even better if you only select the columns you need instead of all columns (`*`). See [https://stackoverflow.com/questions/3639861/why-is-select-considered-harmful] – Rutger Jul 31 '19 at 08:22