This shows you the differences between two versions of the page.
|
goto_the_examples [2009/11/14 16:04] andre |
goto_the_examples [2009/11/14 16:12] (current) andre |
||
|---|---|---|---|
| Line 109: | Line 109: | ||
| And that's it in a nutshell. You'll be connecting to many databases from the same application using the same syntax. I just suffix the database type when I connect to different databases, for instance my Firebird database handle will be $CDEFB and the MySQL database handle will be $CDEMY. | And that's it in a nutshell. You'll be connecting to many databases from the same application using the same syntax. I just suffix the database type when I connect to different databases, for instance my Firebird database handle will be $CDEFB and the MySQL database handle will be $CDEMY. | ||
| + | |||
| + | ===== Executing a Query & Parameters ===== | ||
| + | |||
| + | One of the things I liked about the Firebird PHP library was the ability to pass parameters to the SQL statement, now you don't have to use this but every now and then it makes for clean SQL coding and you may like it. | ||
| + | |||
| + | //Example of a normal update | ||
| + | $CDE->exec ("update tblperson set firstname = 'Andre', lastname = 'van Zuydam' where id = 0"); | ||
| + | $CDE->commit(); //Commit the transaction on a transactional database like Postgres, Firebird, Oracle, MSSQL | ||
| + | |||
| + | //Example of a parameter update | ||
| + | $CDE->exec ("update tblperson set firstname = ?, lastname = ? where id = ?", "Andre", "van Zuydam", 0); | ||
| + | $CDE->commit(); | ||
| + | |||
| + | Now the above examples may not look so different but if it is a complicated SQL statement the second way with parameters comes out cleaner. Another thing you may not want to worry about is passing binary data into a blob and for this reason I use a parameter. | ||
| + | |||
| + | //Example of uploading a file | ||
| + | $CDE->exec ("update tblfile set fileimage = ? where id = 0", file_get_contents("/tmp/somefile.bin")); | ||
| + | $CDE->commit(); | ||
| + | |||
| + | And that should be sufficient to get one started on executing queries to the database. | ||
| + | |||