< Day Day Up > |
7.4 Putting Data into the DatabaseAssuming the connection to the database succeeds, the object returned by DB::connect( ) provides access to the data in your database. Calling that object's functions lets you send queries to the database program and access the results. To put some data into the database, pass an INSERT statement to the object's query( ) function, as shown in Example 7-8. Example 7-8. Inserting data with query( )require 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); } $q = $db->query("INSERT INTO dishes (dish_name, price, is_spicy) VALUES ('Sesame Seed Puff', 2.50, 0)"); Just like with the $db object that DB::connect( ) returns, the $q object that query( ) returns can be tested with DB::isError( ) to check whether the query was successful. Example 7-9 attempts an INSERT statement that has a bad column name in it. The dishes table doesn't contain a column called dish_size. Example 7-9. Checking for errors from query( )require 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); } $q = $db->query("INSERT INTO dishes (dish_size, dish_name, price, is_spicy) VALUES ('large', 'Sesame Seed Puff', 2.50, 0)"); if (DB::isError($q)) { die("query error: " . $q->getMessage( )); } Example 7-9 prints: query error: DB Error: syntax error Instead of calling DB::isError( ) after every query to see if it succeeded or failed, it's more convenient to use the setErrorHandling( ) function to establish a default error-handling behavior. Pass the constant PEAR_ERROR_DIE to setErrorHandling( ) to have your program automatically print an error message and exit if a query fails. Example 7-10 uses setErrorHandling( ) and has the same incorrect query as Example 7-9. Example 7-10. Automatic error handling with setErrorHandling( )require 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("Can't connect: " . $db->getMessage( )); } // print a message and quit on future database errors $db->setErrorHandling(PEAR_ERROR_DIE); $q = $db->query("INSERT INTO dishes (dish_size, dish_name, price, is_spicy) VALUES ('large', 'Sesame Seed Puff', 2.50, 0)"); print "Query Succeeded!";
Example 7-10 prints: DB Error: syntax error Because the program quits when it encounters the query error, the last line of Example 7-10 never runs or prints its Query Succeeded! message. The setErrorHandling( ) function belongs to the $db object, so you have to get a $db object by calling DB::connect( ) before you can call setErrorHandling( ). Therefore, one call to DB::isError( ) is still necessary in your program to see whether the connection succeeded. Once that's taken care of, however, you can call setErrorHandling( ) and not scatter the rest of your program with DB::isError( ) calls. Section 12.4 explains how to have setErrorHandling( ) print out a customized message when there is a database error. Use the query( ) function to change data with UPDATE data as well. Example 7-16 shows some UPDATE statements. Example 7-16. Changing data with query( )require 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); } // Eggplant with Chili Sauce is spicy $db->query("UPDATE dishes SET is_spicy = 1 WHERE dish_name = 'Eggplant with Chili Sauce'"); // Lobster with Chili Sauce is spicy and pricy $db->query("UPDATE dishes SET is_spicy = 1, price=price * 2 WHERE dish_name = 'Lobster with Chili Sauce'"); Also use the query( ) function to delete data with DELETE. Example 7-17 shows query( ) with two DELETE statements. Example 7-17. Deleting data with query( )require 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); } // remove expensive dishes if ($make_things_cheaper) { $db->query("DELETE FROM dishes WHERE price > 19.95"); } else { // or, remove all dishes $db->query("DELETE FROM dishes"); }
The affectedRows( ) function tells you how many rows were changed or removed by an UPDATE or DELETE statement. Call affectedRows( ) immediately after a query to find out how many rows that query affected. Example 7-22 reports how many rows have had their prices changed by an UPDATE query. Example 7-22. Finding how many rows an UPDATE or DELETE affectsrequire 'DB.php'; $db = DB::connect('mysql://hunter:w)mp3s@db.example.com/restaurant'); if (DB::isError($db)) { die("connection error: " . $db->getMessage( )); } // Decrease the price some some dishes $db->query("UPDATE dishes SET price=price - 5 WHERE price > 20"); print 'Changed the price of ' . $db->affectedRows( ) . 'rows.'; If there are five rows in the dishes table whose price is more than 20, then Example 7-22 prints: Changed the price of 5 rows.
|
< Day Day Up > |