Hi to everyone. I need some advices. I need to build an application with PHP and I'm at the next part: I have an array with data and I need to check if the ID from this array is already inserted in my database: - If it's already inserted in DB, I need to get the unique ID of row - If it is not inserted in DB, I need to insert all data of array and get the new unique ID of inserted row. I need to do this job optimized as possible! The table with content where this array will be inserted may have +500.000 rows and I must do this job for 20 arrays, not one (these arrays are data got from a feed source) So, using a foreach loop my pseudocode will be something like: foreach($feed_array as $feed_id){ -check if $feed_id already is in DB - TRUE: get the ID of the row where $feed_id is in - FALSE: insert into db some data (included $feed_id); - get the ID of the row where $feed_id is in } end foreach loop; So this is what I want to do optimized as possible. Thanks you.
//Assumes the rowid is the primary key of the table foreach ($idarray as $id){ $result = mysql_query("SELECT * from feeds WHERE feedid=$id"); if (mysql_num_rows ($result ) > 0){ $row = mysql_fetch_assoc($result); $rowid = $row['rowid']; }else{ mysql_query("INSERT into table() values()"); $rowid = $row['rowid']; $rowid = mysql_insert_id(); } }