phptutorials.cjb.net - Free PHP Tutorials and Advice
 

PHP Installation Tutorial 1 - Installing WAMP! - Page 5 of 5

Test PHP and Apache

Now to see if Apache recognises PHP. Open notepad and save a file called "test.php" (put the quotes around the name insures that notepad saves it as test.php not test.php.txt, notepad sometimes tries to be too helpful!).
In this file type the following:

<?php
echo 'PHP is working.<br>';
echo phpinfo();
?>

Save this file in your doc root (C:\Program Files\Apache Group\Apache2\htdocs).

Open your web browser and navigate to http://localhost/test.php you should see the line 'PHP is working' and a whole load of info about the current PHP configuration.

Test Apache, MySQL and PHP together

Call up a console window (Start > Run > cmd > OK), change to the mysql bin directory (cd c:\mysql\bin) start mysql, logged in as root user (mysql -u root -p <enter> password)
Now we are going to create a (simple) database.

	Enter: CREATE DATABASE simple;
	Enter: USE simple;
	Enter: CREATE TABLE simple_table (
	Enter: 	id	INT AUTO_INCREMENT,
	Enter: 	text	MEDIUMTEXT,
	Enter: 	PRIMARY KEY (id)
	Enter: );
	Response: OK...
	Enter: GRANT ALL
	Enter: ON simple.*
	Enter: TO testuser@localhost
	Enter: IDENTIFIED BY 'testpassword';
	Response: OK...

Create a new file called test_insert_mysql.php and enter he following code into it:

<?php
	// Connect to the database
	$dbhost = 'localhost';
	$dbusername = 'testuser';
	$dbpasswd = 'testpassword';
	$database_name = 'simple';
	$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
		or die ('Couldn\'t connect to server.');
	$db = mysql_select_db("$database_name", $connection)
		or die ('Couldn\'t select database.');

	// Generate SQL code to store data on database.
	$insert_sql = 'INSERT INTO simple_table (text) VALUES (\'test text, 1,2,3\')';

	// Execute SQL code.
	mysql_query( $insert_sql )
		or die ( 'It Didn\'t Work: ' . mysql_error() );

	// Tell User we are done.
	echo 'Code Inserted';
?>

Create a new file called test_select_mysql.php and enter the following code into it:

<?php
	// Connect to the database
	$dbhost = 'localhost';
	$dbusername = 'testuser';
	$dbpasswd = 'testpassword';
	$database_name = 'simple';
	$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
		or die ('Couldn\'t connect to server.');
	$db = mysql_select_db("$database_name", $connection)
		or die('Couldn\'t select database.');

	// Generate code to retrieve data from database.
	$select_sql = 'SELECT text FROM simple_table';

	// Retrieve code from database.
	$result = mysql_query( $select_sql )
		or die ( 'It Didn\'t Work: ' . mysql_error() );

	// Display results to user.
	while ( $row = mysql_fetch_object ( $result ) )
	{
		echo $row->text . '<br>';
	}
?>

Browse to localhost/test_insert_mysql.php, then localhost/test_select_mysql.php. Every time you view test_insert_mysql.php it adds a line to the database, Viewable from test_select_mysql.php. Now even if you reset you computer the data is still stored in the database, this example may not be the most thrilling, but hopefully it gives you a small idea of what is possible with WAMP.

If the above scripts all work as planned, then your WAMP is set up correctly, tune in to the next PHP Tutorial for some actual coding!

Trouble Shooting

In theory, if you have followed the tutorial and I'm as good as I think I am, you should have no problems, but who ever follows every step in the tutorial? and I'm never as good as I think I am, so if you have problems here is what to do:

Download

Download here

Includes source files for PHP scripts discussed above, and a phptutorials recommended php.ini file.

Copyright

Copyright 2003 Matthew Phillips. Permission is granted to copy, distribute and/or modify this document as long as this notice is included. The original version can be found at http://www.phptutorials.cjb.net/ If you make this text available on your website please notify the author, For details of how to notify the author please go to the aforementioned web site and click on the 'Webmasters' link.