📄️ Database Introduction
Now we are going to start working with a database. Any serious application will need some kind of database to store their data.
📄️ Installing MySQL on MacOS
If you are using MacOS, you can install MySQL using Homebrew. Homebrew is a package manager for MacOS. It allows you to install software from the command line. If you don't have Homebrew installed, then you can install it by running the following command in your terminal:
📄️ Installing MySQL on Windows
If you are on Windows and you are using a suite like Laragon or XAMPP, then MySQL will already be installed. If you are not using a suite, then you can download and install MySQL from the MySQL website.
📄️ mysql-shell-queries
MySQL Shell & SQL Queries
📄️ Database Setup in MySQL Workbench
There are a lot of different ways to create and manage your database. You can use the command line, web-based tools like PHPMyAdmin and desktop tools like MySQL Workbench. There are also premium tools like TablePlus. It is up to you on how you want to manage your database. For this course, we will be using MySQL Workbench because it is cross platform and free.
📄️ Create Users & Privileges
Right now, we are using the root user to connect to our database without a password. Which is fine for development, but you never want to do this in production, so I will show you how to create a new user and set a password. I'll show you how to do this within both Workbench and the shell.
📄️ Connect with PDO
In this lesson, we will connect to a database using PDO, which stands for PHP Data Objects. PDO is a PHP extension that provides a consistent interface for interacting with databases. It is a better alternative to the mysql_* functions that were used back in the day. You can use it with other databases as well, such as PostGreSQL, SQLite, and Oracle.
📄️ Select Records
Now that we are connected to our database, let's select some records from our table.
📄️ Select a Single Record from a Database
In the last lesson, we saw how to fetch all records from the database. Now we are going to learn how to fetch a single record. It is actualy very similar except we use the fetch() method instead of the fetchAll() method.
📄️ Insert Records
Now we want to be able to add a new post to the database. We will create a form with a title and body field. When the form is submitted, we will insert the new post into the database.
📄️ Delete Record
We know how to fetch and insert data, now we are going to delete. The issue that we have when working with the browser is that we can't send a DELETE request. We can only send GET and POST requests. To send a true DELETE request, we would need to use JavaScript and AJAX/Fetch.
📄️ Update Records
We want to be able to update a post.