Section 12.2. Manipulating Table Data


12.2. Manipulating Table Data

Since you've practiced executing a few SQL commands that manipulate database objects, you're ready to work with the data in your tables.

12.2.1. Adding Data

Naturally, you'll need to add rows to your tables. To add a purchase to your new purchases table, you'll use an INSERT statement in your query. Example 12-3 shows how this is done.

Example 12-3. Using a predefined INSERT statement in insert.php

 <?php require_once('db_login.php'); require_once('DB.php'); $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database"); if (DB::isError($connection)){ die ("Could not connect to the database: <br />". DB::errorMessage($connection)); } $query = "INSERT INTO `purchases` VALUES (NULL,'mdavis',2,NULL)"; $result = $connection->query($query); if (DB::isError($result)){ die("Could not query the database: <br />". $query." ".DB::errorMessage($result)); } echo "Inserted successfully!"; $connection->disconnect(); ?> 

When you call up insert.php, in your browser, you get:

 Inserted successfully! 

Figure 12-3 shows that the new row made it to the database by selecting all rows from purchases.

Figure 12-3. Validating that our new row is in the database




Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net