Web hosting resellers - CHAPTER 23 INTRODUCING PDO The methods mentioned

CHAPTER 23 INTRODUCING PDO The methods mentioned in the first two bullets are introduced in this section, and those referenced in the third bullet are discussed in the section that follows, Prepared Statements. exec() int PDO::exec (string query) The exec() method executes query and returns the number of rows affected by it. Consider the following example: $query = “UPDATE product SET name=’Painful Aftershave’ WHERE sku=’ZP457321′”; $affected = $dbh->exec($query); echo “Total rows affected: $affected”; Based on the sample data, this example would return: Total rows affected: 1 Note that this method shouldn t be used in conjunction with SELECT queries; instead, the query() method should be used for these purposes. query() PDOStatement query (string query) The query() method executes the query specified by query, returning it as a PDOStatement object. An example follows: $query = “SELECT sku, name FROM product ORDER BY rowid”; foreach ($dbh->query($query) AS $row) { $sku = $row[’sku’]; $name = $row[’name’]; echo “Product: $name ($sku)
“; } Based on the sample data introduced earlier in the chapter, this produces: Product: AquaSmooth Toothpaste (TY232278) Product: HeadsFree Shampoo (PO988932) Product: Painless Aftershave (ZP457321) Product: WhiskerWrecker Razors (KL334899) Tip If you use query() and would like to learn more about the total number of rows affected, use the rowCount() method.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply