Adding Data
Let’s add some data to the table using the code in following example.
<?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 die("Unable to select database: " . mysql_error()); $query = "INSERT INTO cats VALUES(NULL, 'Lion', 'Leo', 4)"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); ?>
You may wish to add a couple more items of data by modifying $query as follows and calling the program up in your browser again:
$query = "INSERT INTO cats VALUES(NULL, 'Cougar', 'Growler', 2)"; $query = "INSERT INTO cats VALUES(NULL, 'Cheetah', 'Charly', 3)";
By the way, notice the NULL value passed as the first parameter? This is done because the id column is of the AUTO_INCREMENT type and MySQL will decide what value to assign according to the next available number in sequence, so we simply pass a NULL value, which will be ignored.
Of course, the most efficient way to populate MySQL with data is to create an array and insert the data with a single query.