slaterino Posted June 12, 2011 Share Posted June 12, 2011 Right, I have a table with two fields. One of these is $event_ID, the other is $object_ID. In php, I am running a while loop for all my $event_ID's taking data from another table. Now, in that while loop all I want to do is search through the table I just mentioned and find any matching $event_ID's. If the $object_ID = 7 I simply then want to echo a little bit of data. What's the best way of doing this? I was thinking that I should create an SQL query, i.e. "SELECT event_ID, object_ID FROM event_object_table WHERE event_ID = '$event_ID', and then running another WHILE loop with an IF clause saying something like: IF ($object_ID = 7) {echo "true";} Is this the best way to do this? Or is there a more efficient way? That's really what I'm after! Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/ Share on other sites More sharing options...
fugix Posted June 12, 2011 Share Posted June 12, 2011 why not simply create a query that searches for both of your conditions. $sql = "SELECT event_ID, object_ID FROM event_object_table WHERE event_ID = '$event_ID and object_ID = 7" then use your while loop to extract whatever data you like Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/#findComment-1228831 Share on other sites More sharing options...
RussellReal Posted June 12, 2011 Share Posted June 12, 2011 it depends on what exactly you're looking to do, if you're looking to pull ALL the results with the object data = '7' well then you're probably best running it in a separate query like: SELECT * FROM events WHERE object_ID = '7' If you only want 1 specific event ID, the object_IDs that equate to 7 will still be pulled regardless, so 2 separate queries are actually inefficient, what you can do, however, (which may be going overboard, unless you're pulling a massive amount of information) you could pull the data from the database and store it in memory (temporary tables) and load the entire sum of the results into that table, then filter out your results as you see fit from the temporary table.. Temporary tables makes accessing the information ALMOST as quick as accessing the information stored in lets say a php variable. Which could offer a performance boost is the amount of results you're pulling is ALOT.. If you're only pulling an average amount of results.. like 30 or 40, your best bet would be to handle it close to the way you're handling it right now, but if you want to STORE them till the end.. you could when you encounter an object_ID 7 result, you can simply store that row in an array, and after the main loop, execute another loop, looping through the storing array, and print them @ the bottom.. I can't rly give you a definite answer as I don't quite understand what you want to do in general. Hope I helped. Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/#findComment-1228834 Share on other sites More sharing options...
slaterino Posted June 12, 2011 Author Share Posted June 12, 2011 Hey, Thanks for the advice. Literally, there will only be 1 or none results so my query is just to find out whether that one result exists or not. So, I think I will do it the way that fugix suggested. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/#findComment-1228836 Share on other sites More sharing options...
RussellReal Posted June 12, 2011 Share Posted June 12, 2011 Hey, Thanks for the advice. Literally, there will only be 1 or none results so my query is just to find out whether that one result exists or not. So, I think I will do it the way that fugix suggested. Cheers! ahh okies Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/#findComment-1228838 Share on other sites More sharing options...
fugix Posted June 12, 2011 Share Posted June 12, 2011 Hey, Thanks for the advice. Literally, there will only be 1 or none results so my query is just to find out whether that one result exists or not. So, I think I will do it the way that fugix suggested. Cheers! a side note, if you are only expecting one result, you will not need to use a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/239171-quick-search-for-variable/#findComment-1228839 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.