This may be a silly question but what am I doing wrong here:
$query="select * from albums, categories_map where enabled=1 and deleted=0 ". if (!empty($_POST[$pop])) { echo ." and category_id = 2 ". } ." order by rand()";
I’m trying to show results dependant on which options are checked on a previous page. That form is then sent using POST
It’s been a LONG time since I’ve really worked with PHP. Do you have a query analyzer or similar that you can check this query on? To make sure the data is there.
I don’t have anything like that, but if I leave the additional ’ AND category_id= ’ queries out then it works. Also if I put the query into phpMyAdmin then I get the results too.
I think the problem is with this part:
$pop=$_POST['pop'];
$classical=$_POST['classical'];
$rock=$_POST['rock'];
$country=$_POST['country'];
$indie=$_POST['indie'];
$easy =$_POST['easy'];
if ($pop =="on"){
$sql_where .= " AND category_id=1 AND albums.id = categories_map.id";
}
if ($classical =="on"){
$sql_where .= " AND category_id=2 AND albums.id = categories_map.id";
}
if ($rock =="on"){
$sql_where .= " AND category_id=3 AND albums.id = categories_map.id";
}
if ($lean=="on"){
$sql_where .= " AND category_id=4 AND albums.id = categories_map.id";
}
if ($indie =="on"){
$sql_where .= " AND category_id=5 AND albums.id = categories_map.id";
}
if ($easy=="on"){
$sql_where .= " AND category_id=6 AND albums.id = categories_map.id";
}
That depends on the number of records to be processed and the number you want to extract from the random list.
If extracting one record from the result it is actually more efficient up to about 700 records. If you are extracting 30 or more then it is more efficient up into the 100000 range.
That it is really expensive is only true if you want a very small number of records from a really huge number of possibilities.
I didn’t know it had any effect on the query at all to be honest. I’ll be getting thousands of results and want the top five to be always the top five so I guess something like order by top_5 rand () would be best, with anything not being in the top_5 field being randomised?