here's an example code $a= array(2, 7, 15, 17); mysql_query("select * from table_name where id=$a"); PHP: i tried to make that command to get values from those tables only whos id r on that array alas! its not working can anyone help me, is there anyway to do such action
following will do help $a= array(2, 7, 15, 17); $id_list = implode(',', $a) mysql_query("select * from table_name where id in ($id_list)"); PHP: if you can explain the purpose, we may be able to pick better approach..
or you can do this: $a= array(2, 7, 15, 17); $num_elements = count($a); //number of elements inside the array for($i=0; $i<$num_elements; $i++) //loop to get all elements { mysql_query("select * from table_name where id=$a[$i]"); //query the elements } PHP:
The only problem with that is that each element is a query on your database (which will be a massive waste of resources if you have say 100 items in the array)
thank you both mastermunj & xenon2010 well, mastermunj...here is my purpose...maybe there's an easy & shortcut way first i want it to collect IDs from a mySQL table, then it will do another mySQL query where id=those collected IDs maybe like this: mysql_query("select id from table_name where user=$userid"); it will collect some IDs where user is my declared $user then it will do another query where it'll select * (all) where id=those collected id & as JAY6390 said...i need something that wont waste massive resources.. thats what i wanted....anymore info need, plz ask me ?? & im really greatful...tnx again
that means you want to select data from 2 tables based on userid. Why not use join in that case? it can be done in single query then. Please share both table structure with us along with proper example, so that we can help building query.
can u giv an example plz 1st one: mysql_query("SELECT id FROM favorites WHERE userid=$userid"); 2nd one: mysql_query("SELECT * FROM data WHERE id=XXXX"); i used XXXX in 2nd query as i dont know what will be there. table structure: table name: favorites field: id, userid (total 2 fields in this table) table name: data field: id, story, author, addedtime, comment (total 5 fields in this table) thats all.
SELECT * from favorites left join data on favorites.id = data.id where userid = $userid Code (markup):
never mind i found it... tnx u heavydev too its working nice now with a single query just what i wanted