Prepared statements are a common thing among databases. They’re useful when you’re going to be running the same query multiple times but with different values inside of it. The common steps when working with prepared statements are as follows
- Declare your query and where the placeholders are.
- Run that prepared statement by just giving data – the data will fall into the placeholders
// our people to add to the database ...
$john = array('john', 34);
$bill = array('bill, 34);
// prepare the query. note the '?' placeholders
$q = 'INSERT INTO person ( name, age) VALUES (?, ?)';
$statement = $mdb2->prepare($q);
// execute the prepared statement with our people
$statement->execute($john);
$statement->execute($bill);
Of course you can rapidly execute lots of data pretty easily by using one array and a foreach to go through it . . .
// our people to add to the database ...
$people = ( ('john', 34),
('bill, 37),
('joe, 32),
('sarah, 55) );
// prepare the query. note the '?' placeholders
$q = 'INSERT INTO person ( name, age) VALUES (?, ?)';
$statement = $mdb2->prepare($q);
// execute the prepared statement with our people
foreach($people as $person) {
$statement->execute($person)
}
Benefit
The benefit to doing it this way, aside from the ‘look at me I’m a PHP/database whiz’ factor, is that it’s faster and easier on the database. Once a query is prepared, the database doesn’t have to parse through the query each time you run that query.
Returning Results
If you need to do a SELECT vs. something like an INSERT or an UPDATE, then you just have to include some parameters to prepare()…
// our people to find in the database ...
$john = array('john', 34);
$bill = array('bill, 34);
// prepare the query. now we need to add a third parameter to prepare()
// which states our return type.
$q = ' SELECT * FROM person WHERE name = ? AND age = ?';
$statement = $mdb2->prepare($q, null, MDB2_PREPARE_RESULT);
// execute the prepared statement with our people
$john_results = $statement->execute($john);
$bill_results = $statement->execute($bill);
That third parameter to prepare() can be either MDB2_PREPARE_RESULT or MDB2_PREPARE_MANIP. Both pretty self explanatory. The second parameter is to declare the data types that the placeholders will be. MDB2 guesses at this automatically so I won’t go into too much detail on it.
For more on MDB2, check out PEAR’s own documentation on it.
gf4e said
Never really got an opportunity to look at MDB2, because have always used my own MySQL class. I kind of do not see how it is faster and easier on the database. We still do a query for every array item. Besides, we call a function for every iteration, which calls other functions in it. Maybe, I do not understand the real use of this kind of giant classes. i just always think that I should not use anything that goes beyong my real needs. It just makes the system heavier and the code more complicated to understand.
George Gonzalez said
Thanks for the comment, gf4e. You bring up good points that represent what is a generally a good philosophy to abide by – keep things lean and free of unneeded bloat.
The specific ‘faster’ and ‘easier’ comments were in reference to prepared statements, not necessarily mdb2 itself. Check out this page: prepared statements on mysql.
I like mdb2 and live with the slight performance hit vs. the built in mysql_* functions for a few reasons:
* once you know the various methods, retrieving data can become simpler. for instance, if you want just one piece of data you would call mdb2->queryOne(). If you want a single dimensional array that has just a column from each row in a result-set, then use queryCol(). the list goes on a bit more.
* what if you end up switching to oracle or another database system that isn’t mysql? (my company actually might do this later) all we’d have to do is change a tiny bit of configuration for mdb2 to use the oracle driver instead of mysql and we’re done. if we had used the built in mysql* functions, we would of had to go through every file that executes queries.
* (just an opinion) the resulting code is cleaner
Of course a lot of it is personal needs and opinion. It sounds like you have a nice mysql class that works for you.