Install a Local PHP Manual with MAMP

I sometimes find myself offline and needing to do web development. As I work mostly with php getting access to the PHP manual can be critical. Without being online the only way to do this is to store a local copy.

Now, I had a few problems getting this to work. Little documentation and a fuzzy head were the main causes. Anyway, I got there in the end so here’s what I did. Pre-requisites are that you have a copy of MAMP (non-pro version) installed and have downloaded and unzipped the manual file.

Copy the manual file somewhere sensible in your localhost root folder. I put mine into /Sites/root/phpmanual/ giving the localhost url of http://localhost/root/phpmanual/php-chunked-xhtml/.

Next, work out which php.ini file you’re using. To do this create and open a phpinfo.php file. A phpinfo.php file just needs the following:

<?php
    phpinfo();
?>

Search for php.ini and you’ll see a line in the row with a heading of Loaded Configuration File. You’ll have a url of something like: /Applications/MAMP/conf/php5.3/php.ini. Open this file in TextWrangler and make the edits below. Make sure you make a copy of the php.ini file before doing this or have some way to restore the original version.

  1. Change display_errors to On.
  2. Change html_errors to On.
  3. Add base url to docref_root and uncomment (delete the semi-colon). In my case the base url is /root/phpmanual/php-chunked-xhtml/.
  4. Uncomment error_prepend_string.
  5. Uncomment error_append_string.
Here’s what they look like in the php.ini file. I’ve added line numbers I get. This is using MAMP Version 2.0.5 and PHP 5.3.2.
277 display_errors = On
311 html_errors = On
319 docref_root = "/ROOT/phpmanual/php-chunked-xhtml/"
320 docref_ext = .html
323 error_prepend_string = "<font color=ff0000>"
326 error_append_string = "</font>"

The final thing to do is save the file and then restart your servers. To test it create a basic php file like the one below.

<?php
include ('file.php');
?>

Launch it and you should see this

Warning: include(file.php) [function.include.html]: failed to open stream: No such file or directory in /Users/zoooj/Sites/root/mailout.php on line 2  
Warning: include() [function.include.html]: Failed opening 'file.php' for inclusion (include_path='.:/Applications/MAMP/bin/php5.3/lib/php') in /Users/zoooj/Sites/root/mailout.php on line 2

Click on the links (in blue) and you’ll be taken to your local copy of the PHP Manual. These links point to my local copy so probably won’t work for you, unless you’ve set it up and used the same location as I have. Simply by regularly updating the manual you can now work offline whenever you want on your php development.

Create MySQL Backup

There are a number of ways to backup a MySQL database. For my local environment I like a quick way to take a backup before trying something new. Rather than go through the cumbersome process of logging into Terminal, navigating to the MAMP /bin directory and then entering a very long command, which I would easily forget, it’s simpler to create a shell script and then invoke it as an Application by using AppleScript.

First, create your shell script by opening a text editor, eg TextWrangler (and not an RTF editor), and type:

#!/bin/bash

On the next line type in the commands you would normally do in Terminal. In my case, I wanted to navigate to the MAMP /bin where mysqldump lives. Then, I wanted to use this to dump a sql file of a specific database within my sites directory.

cd /Applications/MAMP/Library/bin/
./mysqldump -a -uroot -pPASSWORD mydb1 > ../../../../Users/[username]/Sites/Backups/db1.sql;

Finally, close it with

exit

You then save this in a logical place (ie somewhere you can find it!), in my case /Users/[username]/Sites/Backups/scripts/. Next go to Application > Utilities and launch AppeScript Editor. Type the following into the edit pane.

do shell script "/Users/[username]/Sites/Backups/scripts/backup_nsdaukcom_263323_db1.sh"

Save this to either Applications or a sub-directory within Applications. When you change the File Format to Applications before saving.

Once done, all you need to do to take a backup is double-click the new application you’ve created and your previous backup is overwritten with a new one.

Resetting Root Password in MAMP

I struggled for a while to do this as I expected MAMP to automatically update the root password after I’d changed it in the mysql.user table. To be clear, it does not. And so, here’s a brief guide to sorting out you MAMP woes.

First, connect to mysql using Terminal. MAMP is automatically set to use root as the user account to connect to mysql. Funnily enough, root is also the password!

/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
You’ll be logged into mysql and will see the following.
Last login: Wed Dec 28 14:48:04 on ttys001
ZoooJs-MacBook-Pro:~ zoooj$ /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 38
Server version: 5.1.44 Source distribution

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
 The next thing to do is reset your password. Type the following replacing your-new-password with your new password. You should see the Query OK message.
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('your-new-password');
Query OK, 0 rows affected (0.00 sec)
Once that’s done you need to edit a number of files in the /Applications/MAMP/ directory. I still can’t quite believe that you have to do this in so many places. You’d think they would have a single file that has the master password set. Hey ho, maybe not.
So, find and edit these files using a plain text editor, not TextEdit, Word, OpenOffice, etc. Use TextWrangler or another decent Mac IDE. TextWrangler is free. You’ll need to find the line that has -proot and change it to read -pyour-new-password. Note that there is no space between -p (stands for password) and the password itself.

/Applications/MAMP/bin/phpMyAdmin/config.inc.php
/Applications/MAMP/bin/checkMysql.sh
/Applications/MAMP/bin/quickCheckMysqlUpgrade.sh
/Applications/MAMP/bin/repairMysql.sh
/Applications/MAMP/bin/stopMysql.sh
/Applications/MAMP/bin/upgradeMysql.sh
/Applications/MAMP/bin/MAMP/index.php

Restart MAMP and all should be well.