11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 30 • USING <strong>PHP</strong> WITH MYSQLwww.it-ebooks.info$result = $mysqli->query($query, MYSQLI_STORE_RESULT);// Tell the user how many rows have been affectedprintf("%d rows have been deleted.", $mysqli->affected_rows);?>Of course, provided the connecting user’s credentials are sufficient (see Chapter 29 for moreinformation about <strong>MySQL</strong>’s privilege system), you’re free to execute any query you please, includingcreating and modifying databases, tables, and indexes, and even performing <strong>MySQL</strong> administrationtasks such as creating and assigning privileges to users.Recuperating Query MemoryOn the occasion you retrieve a particularly large result set, it’s worth recuperating the memory requiredby that set once you’ve finished working with it. The free() method handles this task for you. Itsprototype looks like this:class mysqli_result {void free()}The free() method recuperates any memory consumed by a result set. Keep in mind that once thismethod is executed, the result set is no longer available. An example follows:$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'corporate');$query = 'SELECT sku, name, price FROM products ORDER by name';$mysqli->query($query);$result = $mysqli->query($query, MYSQLI_STORE_RESULT);// Iterate through the result setwhile(list($sku, $name, $price) = $result->fetch_row())printf("(%s) %s: \$%s ", $sku, $name, $price);// Recuperate the query resources$result->free();// Perhaps perform some other large queryParsing Query ResultsOnce the query has been executed and the result set readied, it’s time to parse the retrieved rows. Severalmethods are at your disposal for retrieving the fields comprising each row; which one you choose islargely a matter of preference because only the method for referencing the fields differs.596

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

Saved successfully!

Ooh no, something went wrong!