Hallo i try to make a simple quiz application in php based on math tests from many years. I have database with such columns:
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.