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.

CHAPTER 30 • USING <strong>PHP</strong> WITH MYSQLwww.it-ebooks.infoinitial process, you might create a web form that accepts up to 20 product names,IDs, prices, and descriptions. Because this information would be inserted usingidentical queries (except for the data, of course), it makes sense to use a boundparameterprepared statement.• Bound results: The bound-result variant allows you to use sometimes unwieldyindexed or associative arrays to pull values from result sets by binding <strong>PHP</strong>variables to corresponding retrieved fields, and then using those variables asnecessary. For instance, you might bind the URL field from a SELECT statementretrieving product information to variables named $sku, $name, $price, and$description.Working examples of both of the preceding scenarios are examined a bit later, after a few keymethods have been introduced.Preparing the Statement for ExecutionRegardless of whether you’re using the bound-parameter or bound-result prepared statement variant,you need to first prepare the statement for execution by using the prepare() method. Its prototypefollows:class mysqli_stmt {boolean prepare(string query)}A partial example follows. As you learn more about the other relevant methods, more practicalexamples are offered that fully illustrate this method’s use.// Create the query and corresponding placeholders$query = "SELECT sku, name, price, descriptionFROM products ORDER BY sku";// Create a statement object$stmt = $mysqli->stmt_init();// Prepare the statement for execution$stmt->prepare($query);.. Do something with the prepared statement// Recuperate the statement resources$stmt->close();// Close the connection$mysqli->close();600

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

Saved successfully!

Ooh no, something went wrong!