0

I'm doing classified table which have featured ad and normal ad they are mixing up

the web will show latest 4 feature ads order by id then show other ads include featured ads and normal ads order by id but not have 4 ads which ready been show as before, but I don't what to use nested query

I have difficulty to create the list of id first 4 ads then exclude them on next query.. here's example which I did simplified, I cut out all other conditions for easy understanding.

$sql = "SELECT adid,title FROM $t_ads where featured='1'";

$featres = mysql_query($sql) or die(mysql_error().$sql);

.. how to create a string list like this $excludeadid = "1,2,3,5,7";

then

$sqlall = "SELECT adid,title FROM $t_ads where adid not in (".$excludeadid.")" ;

or may be use not exit ? or other way to do it right ? best performance

Johan
  • 74,508
  • 24
  • 191
  • 319
binh
  • 21
  • 1
  • 4
  • do you know that `SELECT adid,title FROM $t_ads where featured='1'` is at risk from SQL-injection!!, see this answer: http://stackoverflow.com/questions/5811834/why-would-this-be-poor-php-code/5811853#5811853 to see what's the problem. – Johan May 03 '11 at 10:58
  • also always use mysql_real_escape_string (see here: http://stackoverflow.com/questions/332365/xkcd-sql-injection-please-explain) to see why **and** change this `"SELECT adid,title FROM $t_ads where adid not in (".$excludeadid.")"` (bad) into this `"SELECT adid,title FROM $t_ads where adid not in ('.$excludeadid.')" ` (good) – Johan May 03 '11 at 11:02

1 Answers1

1

Why not do:

$sql = "SELECT adid,title FROM $t_ads where featured <> '1'";

Judging from your code this might be what you want ... Your question has some typos and is difficult to understand, so it's hard to tell.

RedDeckWins
  • 2,111
  • 14
  • 16
  • no the query i did is only simple one, the fact is more complex and it use other few more table joining, the second query need both featured ads and normal ads .. so i cant exclude featured as you mention.. thanks – binh May 03 '11 at 05:47
  • You should post the whole query then – RedDeckWins May 03 '11 at 05:57
  • dear.. i got a solution just do while for puting the id on the list while($row=mysql_fetch_array($featres)) { $excludeadid .= "'".$row['adid']."',";} $excludeadid = trim( $excludeadid,','); SO i got the showed featured ad's ids But now i got other issue ... i can not call the for second time, this query when i need full detail of the ads while($row=mysql_fetch_array($featres)) it return blank result ... have you got any idea ? – binh May 03 '11 at 09:35
  • At last got solution when searching on php.net the if we use the call mysql_fetch_array many time.. remember to put the pointer to the fist of array mysql_data_seek($featres, 0); // point to fist row, because now it in the last when get id of feature ads. now free to use mysql_fetch_array other time – binh May 03 '11 at 09:54
  • @binh, you you found a solution, don't put it in a comment, but answer your own question, so others can see what you did and learn from that. – Johan May 03 '11 at 11:04