Searching a database

What is the best way to have a script search a database and if it finds a match to enter the id number of that line into a specific place in another database?


I want to see if $test is located in the name field of database ‘testdb’ and if it finds it, to enter the id number of of the line in the database where $test was found into the ‘nameid’ field of database ‘targetdb’

Hope that was not too confusing…

Why don’t you tell the real case with real meaning of these “testdb” and “targetdb”?

I haven’t started it yet, so I am tryign to find the best way to do it… That’s why…

But you’re doing it for some purpose? What is it? What is whole case?
Your task is quite unusual - what’s why I ask.

Okay…That’s fair

Here’s a general layout of what I am trying to do. I am able to upload a csv and merge it into a database. The only thing is it was very simplified to work out some of the bugs, before I write it for the actual application.

I need to not only upload and merge the data, but I need to do it across multiple tables through the one csv. After some investigation, it seems that creating a temporary table, uploading the data into it and then distributing the data into the other tables is the only solution I have found.

I am tryign to think through it before I start coding and one of the steps I will need to do is for categories. I need to look to see if the category in the uploaded files is already in the category table and if it is I need to transfer the id code for that manufacturer into the main table so it can be called as necessary and I am not duplicating data.

I just noticed I confused the word database with table in my first post… Sorry for the confusion…

This would just be a simple case of SELECTing and INSERTing.

After you insert the data from the csv file select a row at a time:

SELECT * FROM temp_table

Loop through each and see if it already exists

SELECT * FROM category WHERE id='$temp_cat_id'

If it doesn’t exist then just insert it.

This is obviously not real code and doesn’t contain any security checks. If your temporary table contains lots of rows you might have to do this a block at a time.

Thanks corbyboy… I’ll try it out and let you know.

It’s going to take a little error chasing, but it looks like it will work great… Thanks again.