Learning PHP and MySQL
Example 12-11 Using mysql_insert_id to link up an author to a title
<?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 `books` VALUES (NULL,'Python in a Nutshell',600)";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
$last_value = mysql_insert_id();
echo "The id that was created is: $last_value<br />";
$query = "INSERT INTO `authors` VALUES (NULL,$last_value,'Alex Martelli')";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
echo "Inserted successfully!";
$connection->disconnect();
?>
Posted by on 09/25 at 11:18 AM
Next entry: Example 12-12 Displaying the authors in a list
Previous entry: Example 12-10 The delete.php code for performing a delete