If you also want to be able to actually process the data as well as count it you can do something like:
$query = "SELECT code, txt FROM texts";
if ($stmt = $mysqli->prepare($query);) {
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
// processing of the returned data goes hee - using $stmt->fetch() to retrieve each row
$stmt->free_result();
$stmt->close();
}
I actually did manage to count rows and process data. But not quite the same way you did.
I am going to post my code so you might tell what is the difference between your way and mine, also which one would be more secure.
Selecting all rows in the database just to calculate a count is a highly problematic way to achieve something so simple. Just use a basic select query with a count() function and fetch the first column of the first row.
If itâs only a few then the OP may as well just go straight for using ORDER BY rand() and combine that with LIMIT 1 as for that few itâs not worth worrying about any performance penalty and it saves them an unneeded query
Hi, lets say texts table will be the same as âvideosâ would be for youtube. That means if people use the website, at some point there will be a lot. Hopefully haha.
If you expect it to become very busy, then efficiency may be an issue.
What is the objective of the script? To select just the ânameâ field from one random entry form the table?
That can be done with one query as SpacePhoenix describes.
$sql = $conn->query( "SELECT name FROM texts ORDER BY rand() LIMIT 1 ");
Note this selects only ânameâ. Use * only if you need all columns data.
I wanted to count the total number of rows, so that I can use rand() to get a random number between 0 and total_nb_of_rows, and than I get the txt name for the numver rand gave me.
All of this had one goal: choose a random txt name from texts table.
So my code works, but I really think I have done some bad code, because there must be a much more simple way to do such a request.
Below is my code, that actually does what I want, but I think this is the worst way it could be done, and I can do better. (Once again I am not new to programming, but new to web-dev(php and sql) ).
$sql1 = $conn->query("SELECT * FROM texts");
$row_count= mysqli_num_rows($sql1);
$e= 1;
$max_row= $row_count - $e;
$count= rand(0,$max_row);
$sql = $conn->query( "SELECT * FROM texts WHERE id=$count ");
The query above. ORDER BY RAND() will shuffle the table, LIMIT 1 will limit the number of entries selected to just one. The result should be one random selection, of just the name column.
Tests I did some time ago showed the break even point for retrieving just one random record to be around about 1000 records. If you want to retrieve more than one then the number of records before order by rand should be discarded gets progressively higher - if you want 30 records then order by rand is still more efficient with many millions of records.
Was that just on MySQL only? itâd be interesting to see if the break-even point is similar with different database server software and different storage engines