Search

Subscribe by RSS or Email

 

PHP/MySQL – correct use of INSERT query

May 09

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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 Comments

Add your comment

  1. anonim
    May 16 at 18:00

    Thanks for this, I had always use the first one.

  2. anonim
    May 21 at 09:29

    Really good for beginners that make huge mistakes in their php work.

  3. Andreea
    May 31 at 10:07

    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.

  4. curs valutar
    Jun 05 at 01:33

    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′;

  5. Cristi
    Jul 07 at 11:09

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

  6. butcher
    Jul 11 at 07:22

    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 :)

  7. web design constanta
    Sep 16 at 18:31

    Trebuie sa incerc sa invat si eu php/mysql

Post a comment