11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

www.it-ebooks.infoCHAPTER 30 • USING <strong>PHP</strong> WITH MYSQL$query = 'SELECT name FROM products WHERE price > 15.99';$result = $mysqli->query($query);printf("There are %f product(s) priced above \$15.99.", $result->num_rows);Sample output follows:There are 5 product(s) priced above $15.99.Keep in mind that num_rows() is only useful for determining the number of rows retrieved by aSELECT query. If you’d like to retrieve the number of rows affected by an INSERT, UPDATE, or DELETEquery, use affected_rows(), introduced next.Determining the Number of Affected RowsThis method retrieves the total number of rows affected by an INSERT, UPDATE, or DELETE query. Itsprototype follows:class mysqli_result {int affected_rows}An example follows:$query = "UPDATE product SET price = '39.99' WHERE price = '34.99'";$result = $mysqli->query($query);printf("There were %d product(s) affected.", $result->affected_rows);Sample output follows:There were 2 products affected.Working with Prepared StatementsIt’s commonplace to repeatedly execute a query, with each iteration using different parameters.However, doing so using the conventional query() method and a looping mechanism comes at a cost ofboth overhead, because of the repeated parsing of the almost identical query for validity, and codingconvenience, because of the need to repeatedly reconfigure the query using the new values for eachiteration. To help resolve the issues incurred by repeatedly executed queries, <strong>MySQL</strong> 4.1 introducedprepared statements, which can accomplish the tasks described above at a significantly lower cost ofoverhead, and with fewer lines of code.Two variants of prepared statements are available:• Bound parameters: The bound-parameter variant allows you to store a query onthe <strong>MySQL</strong> server, with only the changing data being repeatedly sent to the serverand integrated into the query for execution. For instance, suppose you create aweb application that allows users to manage store products. To jumpstart the599

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

Saved successfully!

Ooh no, something went wrong!