Skip to main content

Starting out with MAMP, MySQL and PHP

This is a simple post designed to get you started in playing around with getting data in and out of databases. This is an OS X centric post, and if you're following along the first thing you'll need to install is MAMP.

Next we're going to open the MAMP app, which will start the server and take us to an information page with the MAMP logo and details.

From here we're going to select the phpMyAdmin tab from the menu and create a new database that I'm going to call "test" in the "Create new database" box. Once you've done the same click "Create".

Now we need to create a table inside our database and give it a name and let phpMyAdmin know how many fields we want. I've called the table "age" and asked for three fields. Do the same, then press "Go".

Now we have to label these fields and identify the type of content they are going to have. I've called them, "Number", "Name" and "Age". The first and last columns should be set to TINYINT and the middle one to TINYTEXT and set the index column of the "Number" field to UNIQUE.
Now we have our table structure.
 We then click the "Insert" tab and add some data.

Click go and we're done.

Now we need to create a PHP file inside a text editor (like TextWrangler) and save it inside the htdocs folder, which is inside the MAMP application folder. All files need to be saved here when working with MAMP.

The file will be called mysqltest.php and it will contain the following code:





// Enter username and password
$username = root;
$password = root;

// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);

// Identify name of table within database
$table = 'age';

// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);

// Close connection to database
$db = NULL;

while($rows = $stmt->fetch()){
echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";

This is an HTML table inside which is placed some PHP code. This code creates a PHP Data Object connection using the host, username and password that we saw in the MAMP home screen in the very first screenshot above, then queries and fetches before outputting.

I'm not going to enter great detail about this because the post is long enough already, but the comments will guide you through to a certain extent and hopefully you can play around with this very basic code.

Finally we need to view the results. To do this point your browser at: http://localhost:8888/mysqltest.php

The next step is to handle errors.


  1. I've had difficulties with this but this helped a lot and got my u and running. Thank you! :)

  2. This comment has been removed by a blog administrator.

  3. After scouring the internet for hours this finally solved my problem!

  4. Your blog has given me that thing which I never expect to get from all over the websites. Nice post guys!

    SEO Melbourne

  5. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful

    and knowledgeable information.
    PHP training in chennai

  6. Pretty section of content. I simply stumbled upon your site and in accession capital to say that I get actually loved to account your blog posts.
    PHP Training in Chennai

  7. Hi....your article is amazing and thanks for sharing. i read your content thats very informative so please keep it up.
    PHP Training Course in Gurgaon


Post a comment