Selecting random images using php and mysql [duplicate] - php

This question already has answers here:
How to randomly retrieve images from my mysql database?
(3 answers)
Closed 7 years ago.
I have a table containing images path it is like this
id i_id img date
1 0 im.png 2015-05-12
2 0 im1.png 2015-05-12
3 0 im2.png 2015-05-12
And i am trying to echo out images using an src tag in php but what i need to do is that i need to echo out images in random manner and the table will grow also how to do this?. is it a php part or mysql part?.

There are two options. You can either do an ORDER BY RAND(), or you can do a select all and randomize the selected rows in PHP.
Option 1:
SELECT * from IMAGES ORDER BY RAND();
Option 2:
//select images (example uses mysql_ library; it's deprecated so DON'T USE IT!)
$query = "SELECT * FROM IMAGES";
$result = mysql_query($connection, $query)
$rows = mysql_fetch_all($result);
//randomize the array of images
shuffle($rows);
//output the images here
Option 1 can get very slow with large datasets. In my experience, Option 2 is faster, but it would be worth doing your own tests to see what works best for you. Usually the difference will be only fractions of a second. Check the answers here for more information: MySQL: Alternatives to ORDER BY RAND() or here: How can i optimize MySQL's ORDER BY RAND() function?
A third option is to generate random ids and select the albums that match those ids. This is only suitable if you don't have gaps in your ids. I have never tried this.

Related

Select Specific Rows in a Specific Order [duplicate]

This question already has an answer here:
MySQL sort by some list
(1 answer)
Closed 2 years ago.
I have a script (in PHP) that goes through a bunch of different comparisons to generate an ordered array of entries in a table by the row id. Then I'm imploding the array into a string and using WHERE to select those specific rows, however I don't know how to order them in the same order as they were in the array.
$order_array = [50,49,42,52,53,54,51,48,47]
$order_string = implode(',', $order_array);
// echo $order_string returns '50,49,42,52,53,54,51,48,47'
$sql_todo = "SELECT * FROM todo_list WHERE id IN ({$order_string})";
if ($result_todo = mysqli_query($link, $sql_todo)) {
while ($row_todo = mysqli_fetch_assoc($result_todo)) {
This successfully selects the desired rows, but they are not in the same order as the array. I know that I haven't told it to order them that way (so it didn't), but I don't know how to make it happen.
Thanks for your time,
Seth
You could use the field() function:
"SELECT * FROM todo_list WHERE id IN ({$order_string}) ORDER BY FIELD(id, {$order_string})"
field() returns the index of its first argument in the list, which you can directly use for ordering.
Side note: you should probably use a prepared statement rather than concatenating values in the query string (if your values come from outside your code, this is a must-have).

how get random questions from database include Categories?

Hallo i try to make a simple quiz application in php based on math tests from many years. I have database with such columns:
-id
-pytanie
-a
-b
-c
-d
-poprawna
-rok_id
-typ_id
I use rand function to get random id and next i use this in sql answer to get random question.
However id dont know how get radnom question for example random one question include yera_id=1
Firstly i use $numer=rand(1,1800)
My sql select is such as\
select pytanie, a,b,c,d, nazwa, rok_liczba, nazwa_typu, poprawna from pytania left join rok on rok.id= pytania.rok_id left join typ on typ.id=pytania.typ_id where typ_id=1 and pytania.id=".$numer.""
When i add to sql select ,,where year_id=1" i must click many time to hit when rand get 1 beacuse otherwise i dont get any resoult. It possible to rand from records ho are resoult a sql answer ?
You are computing a random number outside of the database then using it as a filter: but there is no guarantee that you have a record that matches your random value and the other filter on the question type.
I would recommend doing the random sort in the query itself. This should be as simple as adding this at the end of your query:
select ...
from ...
where typ_id = 1
order by rand() limit 1
Note that the exact syntax may vary across databases - the above is MySQL syntax.

How to get random tuples from database? [duplicate]

This question already has answers here:
How to randomly select rows in SQL?
(13 answers)
Closed 3 years ago.
I'm working on creating a online quiz program. I want to change questions every time new user logging in. Is there any way to get questions from database randomly?
I have tried rand() function but it generates same question again and again.
You can use NEWID() of MySQL. The SQL SELECT NEWID() function returns the random row. GUID in memory for each row. By definition, the GUID is unique and random so, you will get the perfect random record.
Example:
SELECT * FROM Table ORDER BY NEWID()
TO know more you can click here

different between limit query in mysql and php [duplicate]

This question already has answers here:
How to count all rows when using SELECT with LIMIT in MySQL query?
(5 answers)
Closed 9 years ago.
I'm trying to make page number for my table and need to know row count.
for example my query is SELECT * FROM tbl and the result is 100rows.
I should limit query to 10rows for one page.
if I do that I cant get all rows count.
I want to know is there any different between limit with mysql our php in process speed.
please tell me if you have any idea.
You shoud use LIMIT in your MySQL. Why?
*Because it will take you less time to fetch some amount of data and transmit it instead of getting everything.
*Because it will consume less memory.
*Because working with smaller arrays is generally faster.
As for me, I used PHP as a filter only when I could not perform the filtering in MySQL
Just to update my answer, as Joachim Isaksson already posted:
SQL_CALC_FOUND_ROWS will help you count all the rows, so you can perform a correct pagination.
The best way is probably to use FOUND_ROWS() and combine them both;
SELECT SQL_CALC_FOUND_ROWS * FROM tbl LIMIT 10
...to get the first 10 rows as a result. Then you can just do;
SELECT FOUND_ROWS()
...to get the total number of rows you would have got if you hadn't used LIMIT.
What happens if you will have 100k rows? Of course, you will get rows count, but performance significantly decrease (and memory usage significantly increase). Just use second query to obtain amount of rows and limit rows in mysql
As i understand your problem ,solution might be like this :Strictly by mysql
Use two function first for counting total record
second for getting result
select count(1) as total from table_name
select col1,col2.. from table_name limit $start , $slot_size
Now slot_size is number of record you want to show on page,say 10
start is value which will change according to page

How to echo random row from database? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL: How to retrieve a random row or multiple random rows?
Pull 5 random records from mysql database
I have a large list of domains in my database in the table "list" under the column "website".
There are about 140 million rows and I just need to know how to echo a random one.
Basically like this:
<?php include('directory/database.php'); ?>
WHATEVER CODE WORKS FOR GETTING A RANDOM DOMAIN FROM THE DATABASE
<?php
echo $domain;
?>
There's just a huge amount of records so I need to know the quickest way to just have one of them randomly selected and displayed on a page. Thanks!
You definitely don't want to use ORDER BY RAND(). MySQL has to build a temporary table. If your table has a unique id column, something like this is much better:
SELECT * FROM `table`
WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
ORDER BY id LIMIT 1;
See this blog post for other approaches that work well in php.
Note: This simply repeats my answer on this thread.

Categories