PHP/MySQL – correct use of INSERT query

For all PHP/MySQL developers, there are two ways of using INSERT query:

1. INSERT INTO [table] VALUES (‘value-1′,’value-2’);

2. INSERT INTO [table] (‘column-1′,’column-2’) VALUES (‘value-1′,’value-2’);

Always use INSERT variant 2, because in case you need to add more columns to the database, you won’t need to change the PHP code for the query to work.

The unspecified columns will take default value, defined when appending the new column.

7 thoughts on “PHP/MySQL – correct use of INSERT query”

  1. Mersi ca ai partajat asta.

    Nu sunt atat de tehnica, insa faptul ca exista un blog care sa lamureasca si astfel de probleme e imbucurator.

    Mersi.

  2. Mai e si a 3-a varianta, mai simpla care are sintaxa similara cu update-ul.

    3. INSERT INTO tabel SET camp1=’valoare1′, camp2=’valoare2′;

  3. In php inca se mai lucreaza cu sql query? Nu exista un framework care sa separe query-ul din cod? Hibernate, proceduri stocate ceva?

  4. normal ca exista..tone de frameworkuri, cakephp, codeigniter, symphony, etc care separa sql-ul de cod…mysql 5 suporta si proceduri stocate, triggeri, trazanctii tot ce vrei 🙂

Comments are closed.