Part 5 by: Syion
======================================================
MYSQLWell i figure that you also went to Marc's site and found the MYSQL aspect of it as well. if not the URL for this is:
http://www.entropy.ch/software/macosx/mysql/Here's a little PHP script you can run to see if your MYSQL is working.
Copy the following code into your favorite text editor (like BBEdit), and save the file as test.php within a Web site directory (either /Library/WebServer/Documents/ or /Users/morbus/Sites, for example).
<?
print "<pre>";
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "" );
// select the 'test' database created during installation.
mysql_select_db( "test" ) or die ( mysql_error() . "\n" );
print "Connection to the database has been established.\n";
// create a simplistic table.
$table = "CREATE table wisdom (
id int(4) PRIMARY KEY AUTO_INCREMENT,
wisdom char(255), author char(125) );";
$response = mysql_query( $table, $dbh );
if ($response) { print "The table was created correctly!\n"; }
else { print mysql_error () . "\n"; }
// now, we'll add some data to our newly created table.
// to add different wisdom, just change the 'values'.
$insert_data = "INSERT into wisdom ( wisdom, author )
values ( 'Must... remain... awake!', 'Morbus' );";
$response = mysql_query( $insert_data, $dbh );
if ($response) { print "The data was inserted correctly!\n"; }
else { print mysql_error () . "\n"; }
// and read it back for printing purposes.
$get_table_data = "SELECT * FROM wisdom;";
$response = mysql_query( $get_table_data, $dbh );
if ($response) { print "We successfully got all the table data.\n"; }
else { print mysql_error () . "\n"; }
// now print it out for the user.
while ( $one_line_of_data = mysql_fetch_array( $response ) ) {
extract ( $one_line_of_data );
print "#$id: $author sez: \"$wisdom\"\n";
}
print "</pre>";
?>
Note: Again, we're not going to explore the syntax of the PHP script, or the SQL commands that are used. Suffice it to say that this script will create a table in the MySQL 'test' database, add some data, and then spit back the total contents of the 'wisdom' table.
Here is what you should see. or something similar
Connection to the database has been established.
The table was created correctly!
The data was inserted correctly!
We successfully got all the table data.
#1: Morbus sez: "Must... remain... awake!"
By default, the MySQL root user has no password assigned to it. If you take a gander back at our PHP script, you'll see that we connect to our database with that field blank:
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "" );
The simplest step in beginning to secure our database server is to set a password for MySQL's root user. To do so, enter the following in a Terminal:
mysqladmin -u root password
new_password_hereOnce we do that, we'll have to modify our PHP code as well:
// log into our local server using the MySQL root user.
$dbh = mysql_connect( "localhost", "root", "new_password_here" );
This is just the start of securing a MySQL installation. You can go much deeper, like restricting access to certain databases by host name, much like we restricted access to certain Web directories with Apache's Allow and Deny directives
Well that's it. Good luck all and happy coding.