V
V
Valery Gutin2018-04-27 00:20:43
PHP
Valery Gutin, 2018-04-27 00:20:43

How to display random articles correctly?

Hello.
I want to display other random articles on the article page, I did it as follows:

$randomId = [];

$res = $dbh->query("SELECT * FROM goods WHERE id");
foreach ($res as $key) {
     $randomId[] = $key["id"];
}

I entered all IDs with articles into an array and displayed random ones.
How correct is this approach and how much will it load the database?
ps I have 150 IDs (they don't go one by one)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2018-04-27
@xmoonlight

1 request!
2. Option 2 (faster with a large number of entries): https://stackoverflow.com/a/4329447

S
Sergej, 2018-04-27
@sayber

SELECT * FROM `goods` ORDER BY RAND() LIMIT 10
Do you have more than 500 requests per second to the database? What load are we talking about?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question