How do I connect a MySQL database to my website using PHP?

Hello, I need to connect my MySQL database to my website with PHP. I’m using MyPHPAdmin. I’ve looked through the Codecademy lessons on PHP and SQL, but I didn’t find an explanation there. If I’m wrong about this, please point me to the lesson that explains how to do this. Otherwise, I’d appreciate any resources outside of codecademy.

If these details are helpful in clarifying the question and the limitations of my skills with PHP, I understand that I need to create variables for the server name, username, password, and database name.

$dbServername = “localhost”;
$dbUsername = “me”;
$dbPassword = “myPassword”;
$dbName = “database_name”;

I’ve looked at various tutorials and I’ve seen some people include this in the index document, other people put it on a connect.php document. At this point it gets a bit over my head and I could use a tutorial/lesson that explains all this from square 1. Thanks in advance!


This is the way: (The only proper) PDO tutorial - Treating PHP Delusions

At this stage, as a learning purpose, simply follow the steps on that page.

You can create one file that’ll be connect.php, where you put your credentials, and generate a new PDO connection.

Then in another file (let’s assume you want to display products), called products.php, you can query your database and select some products from your DB.

At the top of products.php, include your connect.php file, like this:


include 'connect.php';

This is basically how it works. Make that work and slowly build from there.

Eventually, you’ll want to put your credentials in a .env file, use PHP’s namespaces to avoid having to include all your different files manually, use the MVC architectural pattern to organize your code, and even use a framework such as laravel to simplify everything and learn best practices.

But for now, this will do : )

Hope this helps and feel free to ask any questions you might have.

Hi, thank you for this. But honestly, it’s way over my head. And I don’t see steps to follow on here. Are you aware of anything in the Codecademy catalogue that explains how to do this?

Not PHP-related, I don’t think they do.

Perhaps this tutorial?

1 Like

This seems much more my speed, thank you. Still, I notice it requires XAMPP and I don’t really know what that is.

In the web development course, I noticed that Codecademy teaches how to connect a database using java script. I was counseled against that approach. Since my project will have an enormous amount of audio files, I was told it would be simpler and would make the website faster to store the files in a MySQL database and connect it to the site using PHPMyAdmin. Did I get bad advice? What are the advantages/disadvantages of using either java script or PHP to connect a database?

XAMPP is a web server solution stack. It’s a convenient development environment as it includes a web server, database, and supports PHP out of the box. So when you develop a project locally (on your computer), you don’t need to worry about installing and setting up Apache, MySQL, PHP, etc. yourself.

Regarding PHPMyAdmin, it’s nothing more than a GUI to manage your database. You don’t exactly need it to connect to your database, but it’s a convenient tool to have a visual interface of your DB.

As regards JavaScript vs PHP, there’s no real difference other than the language itself.

You could do it with JavaScript, you can do it with PHP. The latter might be easier for beginners, mind.

What is this project for, if you don’t mind me asking? School/Uni project, learning purpose/pet project, or real life project/product that will come to production? Is your goal to become a programmer/web dev, or quickly come up with an MVP?

Are you sure that you can’t just use a framework? If you were to use Laravel for example, you wouldn’t need to worry about connecting to MySQL. It does it out of the box for you (among many other things). You can then focus on your business logic, rather than setting everything up.

If it’s to learn how things work, then keep going vanilla (no framework).

This is for some funded research I’m doing. It’s a language documentation website and I made wildly ambitious promises to the granting agency. The less ambitious version of this project would have been to store 10,000 individual audio files (one for for every word in this language) in Soundcloud and link them to the words in the dictionary. But part of what I’m supposed to be doing is creating a model for a later expansion of the project. Currently I’m trying to build a digital dictionary with clickable audio files for every world. But later I want to add some sort of interactive way of presenting the grammar. So eventually there does need to be a back end.

I thought this was going to be way easier than it actually is. The short term goal is just to get this website functional. But I would like to gradually work towards becoming a competent full stack developer.

And I’m not really sure what a framework is. But my webhost is Reclaim Hosting and they have something called C-Panel. There’s already a MySQL database there and PHPMyAdmin. So I think that means XAMPP would be redundant in my case, right?

So a mix of Forvo with dictionary features?

It’s good that you have a webhost available, but I’d imagine it’s a bit of a hassle to develop on there directly?

Which is why we usually build things locally first, then upload to a host. And that’s where XAMPP comes in handy. But you do as you please, whichever seems easier. I’d strongly advise developing locally first, though.

It’s not extremely difficult, but not easy either especially when you’re just getting started. But it’s an awesome project to get acquainted with web / full-stack development!

A framework is the skeleton of your application. It does a lot of things for you out of the box, such as connecting to a database. A lot of code has already been written, so you don’t need to reinvent the wheel, as we say. You don’t have to use one, it’d just make things a whole lot easier down the road, especially if you need to scale this project. But if you want to learn PHP, then first learn it without a framework.

The only real challenge is defining what needs to be stored in the database.

I wouldn’t store the audio files in the database, for example. I’d store them in a folder on the server.

What I’d store in the database is the word itself, perhaps its definition, or link to the dictionary, etc.

1 Like

Yes, like Forvo, but I was also hoping to utilize popup boxes to query other data categories like phonetic transcription, part of speech, and translation.

After looking more closely at the PDO tutorial, I’m going to need to start at the beginning with their introductory tutorials. But I may be able to pull this off. Thanks for your help!

All the best with this project, and glad I could be of help!

Hi, just to follow up, the project isn’t going so well. I’ve scoured the internet for tutorials. Unfortunately introductory tutorials on PHP don’t explain how to connect a database to a website and advanced tutorials assume that I know where to put the blocks of code in the documents in my root directory. I’ve yet to find something that explains how to get the website homepage (index.html) to coexist with the index.php file, or how to get data to show up on an html page. I’m assuming that people don’t typically try use PHP and MySQL databases this way and that’s why I can’t find anything. Though I’m not sure what I should have done instead.