sql - Best way to INSERT autoincrement field? (PHP/MySQL) -


i have insert data 2 tables, items , class_items. (a third table, classes related here, not being inserted into).

the primary key of items item_id, , it's auto-incrementing integer. aside primary key, there no unique fields in items. need know item_id match classes in class_items.

this being done through php interface. i'm wondering best way insert items, , match item_id's class_items. here 2 main options see:

  • insert each item, use mysql_insert_id() item_id class_items insert query. means 1 query every item (thousands of queries in total).
  • get next autoincrement id, lock class_items table can keep adding $item_id variable. mean 2 queries (one items, 1 class_items)

which way best , why? also, if have unlisted alternative i'm open whatever efficient.

the efficient going to use parameterized queries. require using mysqli functions, if you're point of needing optimize kind of query should think being there anyway.

no matter how cut it, you've got 2 inserts make. doing first, grabbing new id value you've described (which imposes insignificant overhead, because value on hand mysql already,) , using in second insert pretty minimal.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -