Brandon - Free Palestine
Brandon - Free Palestine

Reputation: 16656

Inserting data with MySQLi - Should I use prepared statements?

I'm making a function that takes a table name and a key value array and inserts it into my database. I'm wondering if I should use MySQLi prepared statements for this. I'm thinking that sometimes I'll have to insert similar data a bunch of times in a row. For example:

$db->insert('user_meta', array( 'meta_key' => 'title', 'meta_value' => 'Developer' ) );
$db->insert('user_meta', array( 'meta_key' => 'location', 'meta_value' => 'New York' ) );
$db->insert('user_meta', array( 'meta_key' => 'employer', 'meta_value' => 'Subway' ) );
$db->insert('user_meta', array( 'meta_key' => 'language', 'meta_value' => 'English' ) );

If my insert function used prepared statements, would this type of operation go faster?

Upvotes: 0

Views: 383

Answers (3)

magallanes
magallanes

Reputation: 6854

Prepared statements is slower so i don't think that you want to use in a batch process.

http://webdevrefinery.com/forums/topic/10380-database-extension-mysql-mysqli-pdo-benchmarks/

preparing every time is evil. So be careful.

Upvotes: 1

Daniel Ribeiro
Daniel Ribeiro

Reputation: 10234

You should almost always use prepared statements, for pretty much every specific need.

Upvotes: 1

Quentin
Quentin

Reputation: 943193

Yes, that is the purpose of prepared statements.

Upvotes: 1

Related Questions