13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Prepared Statements<br />

281<br />

Let’s consider this code line by line.<br />

When you set up the query, instead of substituting in the variables as done previously,<br />

you put in question marks for each piece of data.You should not put any quotation<br />

marks or other delimiters around these question marks.<br />

The second line is a call to $db->prepare(), which is called mysqli_stmt_<br />

prepare() in the procedural version.This line constructs a statement object or resource<br />

that you will then use to do the actual processing.<br />

The statement object has a method called bind_param(). (In the procedural version,<br />

it is called mysqli_stmt_bind_param().) The purpose of bind_param() is to tell <strong>PHP</strong><br />

which variables should be substituted for the question marks.The first parameter is a format<br />

string, not unlike the format string used in printf().The value you are passing<br />

here (“sssd”) means that the four parameters are a string, a string, a string, <strong>and</strong> a double,<br />

respectively. Other possible characters in the format string are i for integer <strong>and</strong> b for<br />

blob. After this parameter, you should list the same number of variables as you have question<br />

marks in your statement.They will be substituted in this order.<br />

The call to $stmt->execute() (mysqli_stmt_execute() in the procedural version)<br />

actually runs the query.You can then access the number of affected rows <strong>and</strong> close the<br />

statement.<br />

So how is this prepared statement useful? The clever thing is that you can change the<br />

values of the four bound variables <strong>and</strong> re-execute the statement without having to reprepare.This<br />

capability is useful for looping through bulk inserts.<br />

As well as binding parameters, you can bind results. For SELECT type queries, you can<br />

use $stmt->bind_result() (or mysqli_stmt_bind_result()) to provide a list of variables<br />

that you would like the result columns to be filled into. Each time you call $stmt-<br />

>fetch() (or mysqli_stmt_fetch()), column values from the next row in the resultset<br />

are filled into these bound variables. For example, in the book search script you looked<br />

at earlier, you could use<br />

$stmt->bind_result($isbn, $author, $title, $price);<br />

to bind these four variables to the four columns that will be returned from the query.<br />

After calling<br />

$stmt->execute();<br />

you can call<br />

$stmt->fetch();<br />

in the loop. Each time this is called, it fetches the next result row into the four bound<br />

variables.<br />

You can also use mysqli_stmt_bind_param() <strong>and</strong> mysqli_stmt_bind_result() in<br />

the same script.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!