Method | Description | |
---|---|---|
exec($sql_statement) | Executes the specified SQL statement and returns the number of affected rows. If no rows were affected, the method returns zero. |
INSERT
statement$category_id_q = $db->quote($category_id); $code_q = $db->quote($code); $name_q = $db->quote($name); $price_q = $db->quote($price); $query = "INSERT INTO products VALUES (categoryID, productCode, productName, listPrice) ($category_id_q, $code_q, $name_q, $price_q)"; $insert_count = $db->exec($query);
UPDATE
statement$product_id_q = $db->quote($product_id); $price_q = $db->quote($price); $query = "UPDATE products SET listPrice = $price_q WHERE productID = $product_id_q"; $update_count = $db->exec($query);
DELETE
statement$product_id_q = $db->quote($product_id); $query = "DELETE FROM products WHERE productID = $product_id_q"; $delete_count = $db->exec($query);
<p>Insert count: <?php echo $insert_count; ?></p> <p>Update count: <?php echo $update_count; ?></p> <p>Delete count: <?php echo $delete_count; ?></p>
INSERT
, UPDATE
, or DELETE
statement, you use the exec()
method of the PDO object with the SQL statement as the argument.exec()
method returns a value that represents the number of rows that were affected, and this value can be assigned to a variable.