Specifications
FIGURE 10.4
The script completes successfully and reports that the book has been added to the database.
Again, we have connected to the database using mysql_pconnect(), and set up a query to send
to the database. In this case, the query is an SQL INSERT:
$query = “insert into books values
(‘“.$isbn.”’, ‘“.$author.”’, ‘“.$title.”’, ‘“.$price.”’)”;
$result = mysql_query($query);
This is executed on the database in the usual way by calling mysql_query().
One significant difference between using INSERT and SELECT is in the use of
mysql_affected_rows():
echo mysql_affected_rows().” book inserted into database.”;
In the previous script, we used mysql_num_rows() to determine how many rows were returned
by a SELECT. When you write queries that change the database such as INSERTs, DELETEs, and
UPDATEs, you should use mysql_affected_rows() instead.
This covers the basics of using MySQL databases from PHP. We’ll just briefly look at some of
the other useful functions that we haven’t talked about yet.
Other Useful PHP-MySQL Functions
There are some other useful PHP-MySQL functions, which we will discuss briefly.
Accessing Your MySQL Database from the Web with PHP
C
HAPTER 10
10
ACCESSING
YOUR MYSQL
DATABASE
241
13 7842 CH10 3/6/01 3:36 PM Page 241