autoPrepare() and autoExecute() are facilities. With them, you don't have to write anymore these boring (insert or update) sql queries which are difficult to maintain when you add a field for instance.
Imagine you have an 'user' table with 3 fields (id, name and country). You have to write sql queries like:
INSERT INTO table (id, name, country) VALUES (?, ?, ?) UPDATE table SET id=?, name=?, country=? WHERE ...
If you add a field ('birthYear' for example), you have to rewrite your queries which is boring and can lead to bugs (if you forget one query for instance).
autoPrepare
With autoPrepare(), you don't have to write your insert or update queries. For example:
<?php // Once you have a valid DB object named $db... $table_name = 'user'; $table_fields = array('id', 'name', 'country'); $sth = $db->autoPrepare($table_name, $table_fields, DB_AUTOQUERY_INSERT); if (DB::isError($sth)) { die($sth->getMessage()); } ?>
In this example, autoPrepare() will build the following sql query:
INSERT INTO user (id, name, country) VALUES (?, ?, ?)
<?php // ... contining from the example above... $table_values = array(1, 'Fabien', 'France'); $result =& $db->execute($sth, $table_values); if (DB::isError($result)) { die($result->getMessage()); } ?>
So, you don't have to write any sql insert queries! But it works with update queries too. For flexibility reasons, you have only to write the WHERE statement of the query. For instance:
<?php // Once you have a valid DB object named $db... $table_name = 'user'; $table_fields = array('name', 'country'); $table_values = array('Bob', 'USA'); $sth = $db->autoPrepare($table_name, $table_fields, DB_AUTOQUERY_UPDATE, 'id = 1'); if (DB::isError($sth)) { die($sth->getMessage()); } $result =& $db->execute($sth, $table_values); if (DB::isError($result)) { die($result->getMessage()); } ?>
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?