Jump to content

Quick search for variable


slaterino

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.