Jump to content

PHP script, selecting and inserting into mysql without duplicates


thenorman138

Recommended Posts

This actually makes the most sense. The only thing is I'm not sure of is the 'values' part of the insert. Since I've already retrieved everything in the first select statement, I would basically need to insert the values based on index or column name and loop it. What would be the syntax there, as opposed to explicit values like you showed?

Depends on exactly how you retrieve them in the query, but you'd likely build it something like

 

$insert[] = "({$row['sessionid']}, {$row['name']}, {$row['number']})";

 

within the loop, for each row, then implode() that $insert array with a comma value when you're ready to tack it on to the end of the actual query.

 

$query = "INSERT INTO proddb.tablename (sessionid, name, phone, ...) VALUES " . implode(',', $insert);

 

Include your actual column names and make sure the number of columns listed matches the number of values in each VALUE () element and all that jazz. This is just an example.

 

-John

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.