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.

280 Chapter 11 Accessing Your <strong>MySQL</strong> Database from the <strong>Web</strong> with <strong>PHP</strong><br />

$author = addslashes($author);<br />

$title = addslashes($title);<br />

$price = doubleval($price);<br />

}<br />

Because the price is stored in the database as a float, you don’t want to put slashes into<br />

it.You can achieve the same effect of filtering out any odd characters on this numerical<br />

field by calling doubleval(), which we discussed in Chapter 1,“<strong>PHP</strong> Crash Course.”<br />

This also takes care of any currency symbols that the user might have typed into the<br />

form.<br />

Again, you connect to the database by instantiating the mysqli object <strong>and</strong> setting up<br />

a query to send to the database. In this case, the query is an SQL INSERT:<br />

$query = "insert into books values<br />

('".$isbn."', '".$author."', '".$title."', '".$price."')";<br />

$result = $db->query($query);<br />

This query is executed on the database by calling $db->query() (or mysqli_query() if<br />

you want to do things procedurally).<br />

One significant difference between using INSERT <strong>and</strong> SELECT is in the use of<br />

mysqli_affected_rows().This is a function in the procedural version or a class member<br />

variable in the object-oriented version:<br />

echo $db->affected_rows." book inserted into database.";<br />

In the previous script, you used mysqli_num_rows() to determine how many rows were<br />

returned by a SELECT.When you write queries that change the database, such as<br />

INSERTs, DELETEs, <strong>and</strong> UPDATEs, you should use mysqli_affected_rows() instead.<br />

We’ve now covered the basics of using <strong>MySQL</strong> databases from <strong>PHP</strong>.<br />

Using Prepared Statements<br />

The mysqli library supports the use of prepared statements.They are useful for speeding<br />

up execution when you are performing large numbers of the same query with different<br />

data.They also protect against SQL injection-style attacks.<br />

The basic concept of a prepared statement is that you send a template of the query<br />

you want to execute to <strong>MySQL</strong> <strong>and</strong> then send the data separately.You can send multiple<br />

lots of the same data to the same prepared statement; this capability is particularly useful<br />

for bulk inserts.<br />

You could use prepared statements in the insert_book.php script, as follows:<br />

$query = "insert into books values(?, ?, ?, ?)";<br />

$stmt = $db->prepare($query);<br />

$stmt->bind_param("sssd", $isbn, $author, $title, $price);<br />

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

echo $stmt->affected_rows.' book inserted into database.';<br />

$stmt->close();

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

Saved successfully!

Ooh no, something went wrong!