How to connect a database using MAMP?

I am confused which one to use… our class was instructed to download MAMP and MySQL workbench… so which am I gonna use for database? the one from MAMP or from MySQL Workbench?

if so, what should be the format/syntax of connecting to a database?

Download and install Mamp first. Once installed start it running.
Then, open a browser and type in “localhost” for the address. No quotes of course.
This will bring you to the Mamp main control page. In there, you should see MyPHPadmin for your DB.
click on that and you should be in your MySQL control page and can create databases, tables, whatever…
The workbench can create databases, but, it is much easier just to use the MySQL control panel instead.
I suggest for you and testing, do not enter a password, just leave it blank as it is not needed.

what should be the syntax in connecting a database using PHP?

Well, for PDO, something like this might work. If you set up your database with just the root and no password, this is how you would connect to it. If you want to use MySQLi, it would be different!

// Set up PDO ( for testing Wamp or Mamp, root access, no password )
$PDO = new PDO(“mysql:host=“localhost”;dbname=your-database-name”, “root”, “”);
// set the PDO error mode to exception

That simple. Then, to access queries, you use the $PDO as the start of the query commands. Easy

what should be the syntax if I use MySQLi?
when can I use the workbench then?

$mysqli = new mysqli("localhost","root","","your-database-name");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;

Or something like that. PDO is what most programmers prefer these days. It is less lines and more secure.

Workbench is an application. ( A program! ) You install it, run it and connect it to your database. It has help functions that can walk you thru it, but, it is quite easy to set up.

if I use the workbench, will I use PHP too?

as for the database that I have downloaded, should I save it to MAMP root so that I can see it when I connect to it?

my database is in .sql file… is it right?

Why do I have errors like this always? I have been following the same codes from the web and this is what I get all the time.

Well, you can not use either PHP nor the Workbench without a database. Well, you can use PHP, but, not attach it to any database unless the database exists.

If you have a SQL file, a database that you downloaded, you start Mamp so it is running. Then, bring up a browser. Then, enter localhost as the URL address. Then, in there, select PhpMyAdmin and once there, you can IMPORT your SQL file. It should create the database table for you. Some SQL files will also create the database for you, but, most just the tables inside the database. Once that is in place, then you can program PHP to attach to it or attach the Workbench to it.

That error is from a connection error. But, what code did you use to get there? If you used my example, you had to change your database name to whatever you created. You can NOT connect to a database that does not exist.

I have managed to add a user in phpMyAdmin and the first error is now gone

my error now is unknown database

this is my code


$mysqli = new mysqli("localhost", "admin","root","sakila.sql");

// Check connection

if ($mysqli -> connect_errno) {

  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;




in the first image, you can see the database name is sakila ! Not sakila.sql. so, take out the .sql in the connection line.

1 Like

yeah… I just found it out just recently… I thought that you have to put the extension file… thank you so much for the time and effort.

Also, it is new mysqli(“localhost”, “username”, “password”, “database name”);

So, for yours, it looks like you created a username of “admin” and a password of “root”.
If so, good, if not fix them, too.

so if I created my own username and password, should I not use new mysqli?

You still need that. Just making sure you understand what you did.
The NEW means a new connection, not a new database.

You created a database. This will connect to it using a NEW connection. Then, you can use this connection in the program to do whatever you want to do with the database. Of course, that is another matter. This is just the connection to the database. Have to get to bed, so that is for another day…

I just created a new user on the workbench but not on phpMyAdmin… there’s no error… so I am wondering… does MAMP use the workbench’s MySQL?

Once you link the front-end-app Workbench to a database, it allows you to do anything you want with it, so, the answer is NO. Mamp does not use Workbench’s anything. BUT, Workbench uses Mamp’s SQL system completely.

Sponsor our Newsletter | Privacy Policy | Terms of Service