...

View Full Version : Different ways to use Prepared Statements?



doubledee
09-03-2011, 06:17 PM
Are there different ways to use Prepared Statements in PHP?

I have been using code like this...


// Prepare statement.
$stmt = mysqli_prepare($dbc, $q);

// Bind variable.
mysqli_stmt_bind_param($stmt, 's', $prettyTitle);

// Execute query.
mysqli_stmt_execute($stmt);

// Store result-set.
mysqli_stmt_store_result($stmt);


...but it seems to me that some seems passionate about PDO? :confused:

Or maybe there are even other ways?


**NOTE: I am not a object-oriented type person because I am a wimp, so I'd sorta like to stay with a Procedural approach unless someone can give me compelling reasons to switch?!



Debbie

Spookster
09-03-2011, 11:50 PM
PDO (PHP Data Objects) allow you to use prepared statements. Prepared statement allow the database to retain some of the information about your query statement so the next time it executes your query it doesn't have as much work to do. You would want to use that for queries you execute alot where the only thing that changes are your parameters.

PDO is a database abstraction layer in PHP. It allows you to switch between different database types with little or no code changes because none of the calls to it's API are database specific except for the initial database connection call that specifies which type of database you are using.

doubledee
09-04-2011, 03:09 AM
PDO (PHP Data Objects) allow you to use prepared statements. Prepared statement allow the database to retain some of the information about your query statement so the next time it executes your query it doesn't have as much work to do. You would want to use that for queries you execute alot where the only thing that changes are your parameters.

PDO is a database abstraction layer in PHP. It allows you to switch between different database types with little or no code changes because none of the calls to it's API are database specific except for the initial database connection call that specifies which type of database you are using.

So PDO and Prepared Statements are mutually exclusive?


Debbie

Spookster
09-04-2011, 10:31 PM
So PDO and Prepared Statements are mutually exclusive?


Debbie

PDO is not there just for the purpose of using prepared statements. It's a database abstraction layer. It does alot more than just that. You can use 1 of 3 different database APIs in PHP for MySQL. (Original MySQL API, MySQLi or PDO). MySQL and MySQLi are APIs for MySQL where PDO can be used with any of the supported databases. You can use prepared statements with MySQLi as well.

Prepared statements are a feature of databases in general. You can use prepared statements with many other languages also.


http://www.php.net/manual/en/intro.pdo.php
http://php.net/manual/en/pdo.prepared-statements.php
http://php.net/manual/en/mysqli.prepare.php

kbluhm
09-06-2011, 02:30 AM
Without switching over to the OOP-enhanced syntax, what you have there is pretty much on target.

The link below will show a number of different ways to bind parameters to a prepared statement using the OOP syntax.

http://www.php.net/manual/en/pdostatement.bindparam.php

As you can see in the example code in that link, you can bind parameters using names, such as :colour, :calories, etc... or numerically using question marks to specify parameter location and then target them by their numerically sequential position.

Slightly off topic, PDO is not a database abstraction. There is no query builder, and it cannot compensate for proprietary syntax or missing features between different types of RDBMS. There is more than a slight difference between data-access abstraction and database abstraction. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum