Sameer Zahid
Sameer Zahid

Reputation: 583

Using Php PDO Insert statement

Someone told me that when you are working with PDO, you cannot use "INSERT INTO .... SET" to insert data into database, because it will not work on databases other than MySQL. I'm not sure what exactly he means, maybe he means I should use the other method of inserting like,

INSERT INTO table (column1, column2) VALUES (?, ?)

I tried searching on the internet for this, but I couldn't find anything. Please let me know about this.

Thank you.

Upvotes: 3

Views: 468

Answers (4)

Mike Brant
Mike Brant

Reputation: 71422

The INSERT INTO ... SET syntax is not part of the ANSI SQL standard and therefore is not supported as widely across different RDBMS implementations. If you are designing your application such that it is tightly coupled to MySQL, using this syntax would be OK. If you are trying to design such that your application is not tightly coupled with the RDBMS implementation, then you should use the more standard INSERT INTO table (columns) VALUES (values) syntax.

Upvotes: 0

anon
anon

Reputation:

As stated in this the only metioned difference, b/n mysql driver and others is stated below. Suppose this is the a simple, query:

<?php
$stmt = $db->query('SELECT * FROM table');
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';

Now, let's read how the documentation states how/why that query can return count of affected_rows only when the mysql driver

NOTE: Though the documentation says this method is only for returning affected rows from UPDATE, INSERT, DELETE queries, with the PDO_MYSQL driver (and this driver only) you can get the row count for SELECT queries. Keep this in mind when writing code for multiple databases.

This is because MySQL's protocol is one of the very few that give this information to the client for SELECT statements. Most other database vendors don't bother divulging this information to the client as it would incur more overhead in their implementations.

So, there is that little difference, as far as I know.

Upvotes: 0

vonUbisch
vonUbisch

Reputation: 1469

You should use the INSERT INTO table (column1, column2) VALUES (?, ?) statement instead of the INSERT INTO table SET column1=? statement, because that's the correct syntax for SQL based database languages. Although MySQL accepts it, others may not.

Upvotes: 1

Erik Nedwidek
Erik Nedwidek

Reputation: 6184

Positional parameters in PHP PDO is just fine. The other option is named parameters. If I remember correctly from what I've seen in the PDO C code, it is PHP and not the DBM that does the replacements.

http://php.net/manual/en/pdo.prepared-statements.php

Upvotes: 0

Related Questions