You are now ready to look at some practical techniques that you can use in PHP to access the MySQL database, including tasks such as creating and dropping tables, inserting, updating, and deleting data, and protecting your database and website from malicious users. Note that the following examples assume that youâve created the login.php program discussed earlier in this chapter.
Creating a Table
Letâs assume that you are working for a wildlife park and need to create a database to hold details about all the types of cats it houses. You are told that there are nine families of cats: Lion, Tiger, Jaguar, Leopard, Cougar, Cheetah, Lynx, Caracal, and Domestic, so youâll need a column for that. Then each cat has been given a name, so thatâs another column, and you also want to keep track of their ages, which is another. Of course, you will probably need more columns later, perhaps to hold dietary requirements, inoculations, and other details, but for now thatâs enough to get going. A unique identifier is also needed for each animal, so you also decide to create a column for that called id.
ExampleÂ 10-9 shows the code you might use to create a MySQL table to hold this data, with the main query assignment in bold text.
<?php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or ...