hi guys.. masters... i have this problem deleting more than 1 record. my desired outcome should be: mysql_query("DELETE FROM employee WHERE emp_id IN (2,5)"); 2 and 5 are from foreach function .... is this possible? mysql_query("DELETE FROM employee WHERE emp_id IN (" . foreach ($values as $a) { $a; --$counter; if ($counter != 0) { . ',' . ; } else { .')'. ; }} ); it wont work... im been working on this for 2 days... whew.. my brain aches... i just want to get the value from an array so i suppose to use foreach function for that and put it on the delete query but it wont work... please help... thank you in advance
Eric has it right, you can't run procedural code (like foreach) as a function... which is what you are trying to do there. You need to build the string first... Though as also suggested, the IMPLODE function would mean you wouldn't need to waste time on foreach or the overhead of all those extra values... though his example makes no sense in context. mysql_query(' DELETE FROM employee WHERE emp_id IN ('.implode($values,',').') '); Code (markup): Should be all you need. Though to do it how you were trying, you'd have to run the foreach BEFORE the mysql_query function. Foreach is a procedural language construct -- you cannot use those as parameters to a procedure, you can only use functions for that. $queryValues=''; foreach ($values as $a) { $queryValues.=( $queryValues=='' ? '' : ',' ).$a; } mysql_query(" DELETE FROM employee WHERE emp_id IN ($queryValues) "); Code (markup): But really that's brute-forcing something PHP already has a function (implode) to handle for you.
sir eric and sir deathshadow... thank you very much.. il try to use implode now.. i never use it before thats why ive try different ways .. i know there must be other ways /function to do it much easier but i dont what is it.. thanks... il try now...