/php-planetscale

Primary LanguagePHPCreative Commons Zero v1.0 UniversalCC0-1.0

Learn how to integrate PlanetScale with a sample PHP application

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.

Prerequisites

Set up the PHP app

  1. Clone the starter application using the command below:
git clone https://github.com/yemiwebby/php-quickstart.git
  1. Navigate into the folder and install the dependencies:
cd php-quickstart
composer install
  1. Copy the .env.example file into .env:
cp .env.example .env
  1. Start the application:
php -S localhost:8000

View the application at http://localhost:8000.

Connect to the PHP app

Make sure you have your PlanetScale database set up. You can find detailed instructions on setting up in the PlanetScale PHP documentation.

Connect with username and password

  1. In the PlanetScale dashboard, click on the the branch you want to connect to (we're using main).
  2. Click "Connect", and select "PHP" from the language dropdown.
  3. Copy the credentials.
  4. Open the .env file 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=
  1. 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

Adding the schema and data

  1. Go to your PlanetScale dashboard and select your PHP database.
  2. Click on the "Branches and select the main branch (or whatever development branch you used).
  3. Click on "Console"
  4. Create the categories table:
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;
  1. Create the products table:
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;
  1. Add data to the products table 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');
  1. Add data to the categories table with:
INSERT INTO `categories` (name, description) VALUES
('Clothing', 'Description for Clothing'),
('Electronics', 'Description for Electronics'),
('Appliances', 'Description for Appliances'),
('Health', 'Description for Health');
  1. 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.

Need help?

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.