Note: This tutorial targets PlanetScale Vitess/MySQL. PlanetScale also offers managed Postgres. For more information, see the PlanetScale Postgres documentation.
This sample application demonstrates how to connect to a PlanetScale MySQL database from a PHP application
For the full tutorial, see the PHP PlanetScale documentation.
- PHP — This tutorial uses
v8.1 - Composer
- A free PlanetScale account
- Clone the starter application using the command below:
git clone https://github.com/yemiwebby/php-quickstart.git- Navigate into the folder and install the dependencies:
cd php-quickstart
composer install- Copy the
.env.examplefile into.env:
cp .env.example .env- Start the application:
php -S localhost:8000View the application at http://localhost:8000.
Make sure you have your PlanetScale database set up. You can find detailed instructions on setting up in the PlanetScale PHP documentation.
- In the PlanetScale dashboard, click on the the branch you want to connect to (we're using
main). - Click "Connect", and select "PHP" from the language dropdown.
- Copy the credentials.
- Open the
.envfile in your app and replace the placeholders with the values you copied:
HOST=<ACCESS_HOST_URL>
DATABASE=<DATABASE_NAME>
USERNAME=<USERNAME>
PASSWORD=<PASSWORD>
MYSQL_ATTR_SSL_CA=
- For
MYSQL_ATTR_SSL_CA, use our CA root configuration doc to find the correct value for your system. For example, if you're on MacOS, it would be:
MYSQL_ATTR_SSL_CA=/etc/ssl/cert.pem
- Go to your PlanetScale dashboard and select your PHP database.
- Click on the "Branches and select the
mainbranch (or whatever development branch you used). - Click on "Console"
- Create the
categoriestable:
CREATE TABLE categories (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;- Create the
productstable:
CREATE TABLE products (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
image VARCHAR(255) NOT NULL,
category_id INT NOT NULL,
PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;- Add data to the
productstable with:
INSERT INTO `products` (name, description, image, category_id) VALUES
('Shoes', 'Description for Shoes', 'https://via.placeholder.com/150.png', '1'),
('Hat', 'Description for Hats', 'https://via.placeholder.com/150.png', '1'),
('Bicycle', 'Description for Bicycle', 'https://via.placeholder.com/150.png', '4');- Add data to the
categoriestable with:
INSERT INTO `categories` (name, description) VALUES
('Clothing', 'Description for Clothing'),
('Electronics', 'Description for Electronics'),
('Appliances', 'Description for Appliances'),
('Health', 'Description for Health');- You can confirm that it was added by running:
SELECT * FROM products;
SELECT * FROM categories;You can now refresh the PHP homepage to see the new record.
If you need further assistance, you can reach out to PlanetScale's support team, or join our GitHub Discussion board to see how others are using PlanetScale.