Running Linux, 5th Edition - Matthias Kalle Dalheimer [453]
mysql> INSERT INTO comment_table VALUES ('0','comment1');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM comment_table;
+----+----------+
| id | comment |
+----+----------+
| 1 | comment |
| 2 | comment1 |
+----+----------+
2 rows in set (0.00 sec)
As you can see, we have specified 0 as the value for the first column again, but MySQL has automatically selected the next available valid value.
At this point, you know already enough about MySQL to experiment yourself or start reading another book about databases and dream about building the next hugely successful e-commerce web site.
But before you enter your dream, take a moment and let us finish our MySQL discussion by letting you in on one more useful feature: SQL scripts.
You do not necessarily need to type in all commands at MySQL's own command-line prompt. You can also execute batch files with SQL commands by piping them to the mysql program. For example, if you save the following SQL code as create_db.sql:
DROP DATABASE IF EXISTS test_database;
CREATE DATABASE test_database;
USE test_database;
CREATE TABLE comment_table( id INT NOT NULL auto_increment,\
comment TEXT,PRIMARY KEY(id));
INSERT INTO comment_table VALUES ('0','comment');
INSERT INTO comment_table VALUES ('0','comment1');
you can execute this script from the ordinary Linux command line with:
mysql -u root -p < create_db.sql
The line:
DROP DATABASE IF EXISTS test_database;
is of course pretty dangerous; you should use it only if you don't have important data in your database.
To tell the truth, it is not absolutely necessary (albeit strongly recommended) to create a new database for each project. In theory, you could lump all your data into the test database that is preinstalled with MySQL as long as you make sure the table names are all different. In practice, this would be a maintenance nightmare if you had more than a handful of tables.
* * *
[*] This is not a real SQL command, but rather a MySQL administration command.
PHP
To complete our combo of Linux, Apache, PHP, and MySQL, we still need the PHP language interpreter. PHP is a recursive acronym that expands to PHP: Hypertext Preprocessor. It has been in development for several years now; the versions most commonly used are Version 4 and Version 5. We use PHP4 in this chapter, because it was the most often used version at the time of writing. The changes between Versions 4 and 5 are either in underlying implementation or advanced features that will interest you only when you pile up a large number of PHP files.
Some Sample PHP
One of the nice things about PHP is that PHP code can be entered directly into HTML code. The web server will pass everything between to the PHP module, which will interpret and execute the commands. Here is a very simple example for some PHP code in an HTML page; if you already have set up PHP, you could run this directly from your web server (if not, we'll tell you how to set up PHP shortly):
echo
"Hi, ";
?>
LAMP enthusiasts.
As you probably already have expected, your browser will output the following text:
Hi, LAMP enthusiasts.
This extremely simple example shows how Apache works together with the PHP interpreter: the code between is passed to the PHP interpreter, which executes the echo command, which in turn outputs its parameters to the web browser. In addition to this, the line LAMP enthusiasts is simply added as ordinary HTML text (and since it doesn't have any markup, it doesn't look like HTML).
Of course, PHP can do much more. Like most programming languages, it can use variables and make decisions, as in the following script