Part 2: Building a Database-Driven Application with Claude Code, PHP & MySQL
Welcome back! In Part 1, we covered the basics of using Claude Code to build static websites. Now we're taking it to the next level: building a complete database-driven application using PHP and MySQL. By the end of this tutorial, you'll have a fully functional task checklist application running on your Mac.
What You'll Learn
- Setting up a local PHP and MySQL development environment on macOS
- Configuring MySQL for easy command-line access
- Using Claude Code to build a database-driven PHP application
- Running a local development server to test your work
- Best practices for database security and connection management
Prerequisites
Before starting, make sure you have:
- A Mac running macOS (Monterey or later recommended)
- VSCode with Claude Code extension installed (see Part 1)
- Basic understanding of HTML and how websites work
- Familiarity with the Terminal application
Step 1: Install PHP
Modern macOS versions come with PHP pre-installed, but it might be an older version. Let's check what you have:
php -v
If you see PHP 8.0 or later, you're good to go! If not, or if you prefer using Homebrew for package management, install the latest PHP:
brew install php
After installation, verify again with php -v. You should see something like PHP 8.3.x.
Step 2: Install MySQL
MySQL is the database system we'll use to store our application data. Here's how to install it using Homebrew:
# Install MySQL
brew install mysql
# Start MySQL service
brew services start mysql
# Secure your MySQL installation
mysql_secure_installation
During mysql_secure_installation, you'll be asked several questions:
- Set root password: Choose a strong password and remember it!
- Remove anonymous users: Yes
- Disallow root login remotely: Yes
- Remove test database: Yes
- Reload privilege tables: Yes
Step 3: Configure MySQL for Easy Access
Typing your MySQL password every time gets tedious. Let's set up a .my.cnf file for easier access during development:
# Create the configuration file
nano ~/.my.cnf
Add these lines (replace your_password with your actual MySQL root password):
[client]
user=root
password=your_password
host=localhost
[mysql]
database=
[mysqldump]
user=root
password=your_password
Save and exit (Ctrl+X, then Y, then Enter), then secure the file:
chmod 600 ~/.my.cnf
Important Security Note: The .my.cnf file stores your password in plain text, so it's only suitable for local development. Never use this approach on production servers or commit this file to version control!
Now you can access MySQL simply by typing:
mysql
No password prompt needed! Try it out, then exit with exit or quit.
Step 4: Create Your Project Database
Let's create a database for our task checklist application:
mysql -e "CREATE DATABASE task_checklist CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -e "SHOW DATABASES;"
You should see task_checklist in the list of databases.
Step 5: Set Up Your Project Directory
Create a new folder for your project:
mkdir ~/Projects/task-checklist
cd ~/Projects/task-checklist
Open this folder in VSCode:
code .
Step 6: Use Claude Code to Build the Application
Now comes the fun part! Open Claude Code in VSCode and give it this detailed prompt:
"Create a task checklist application using PHP and MySQL with the following features:
1. A database table called 'tasks' with columns: id (auto-increment), title (varchar 255), description (text), completed (boolean), created_at (timestamp)
2. Create a config.php file for database connection with these credentials:
- host: localhost
- database: task_checklist
- username: root
- password: [your MySQL password]
3. Create an index.php file with:
- A form to add new tasks
- Display all tasks in a nice table
- Checkbox to mark tasks as complete/incomplete
- Delete button for each task
- Modern, clean CSS styling
4. Create AJAX handlers for adding, updating, and deleting tasks without page refresh
5. Include proper error handling and SQL injection prevention using prepared statements
6. Add a SQL file (schema.sql) to create the database table"
Claude Code will generate all the necessary files. You'll typically get:
config.php- Database connection configurationschema.sql- Database table structureindex.php- Main application interfaceapi.phpor separate handlers - AJAX endpoints for CRUD operationsstyles.css- Styling for your application
Step 7: Initialize the Database
Claude Code will have created a schema.sql file. Import it into your database:
mysql task_checklist < schema.sql
Verify the table was created:
mysql task_checklist -e "DESCRIBE tasks;"
Step 8: Start Your Development Server
PHP includes a built-in development server that's perfect for testing. Start it with:
php -S localhost:8000
You'll see output like:
[Thu Oct 31 16:00:00 2025] PHP 8.3.0 Development Server (http://localhost:8000) started
Now open your browser and visit:
http://localhost:8000
You should see your task checklist application running! Try adding tasks, marking them complete, and deleting them.
Step 9: Iterating with Claude Code
The beauty of Claude Code is how easy it is to enhance your application. Try asking for improvements:
- "Add a priority field to tasks (low, medium, high) with color coding"
- "Add a due date field with calendar picker"
- "Sort tasks by completion status and creation date"
- "Add a search/filter feature"
- "Make the interface mobile-responsive"
- "Add user authentication with login/logout"
Claude Code will modify your existing files and explain the changes it made.
Understanding the Code
Let's break down the key components Claude Code created:
Database Connection (config.php)
<?php
$host = 'localhost';
$db = 'task_checklist';
$user = 'root';
$pass = 'your_password';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
This uses PDO (PHP Data Objects) for secure database connections and prepared statements to prevent SQL injection.
Prepared Statements for Security
// Safe way to insert data
$stmt = $pdo->prepare("INSERT INTO tasks (title, description) VALUES (?, ?)");
$stmt->execute([$title, $description]);
Never concatenate user input directly into SQL queries! Prepared statements automatically escape dangerous characters.
Development Tips
Debugging PHP
Add these lines to the top of your PHP files during development:
error_reporting(E_ALL);
ini_set('display_errors', 1);
This shows detailed error messages in your browser.
Checking Logs
If something goes wrong, check the PHP development server output in your terminal. You'll see HTTP requests and any errors.
Database Management Tools
Consider installing a GUI tool for MySQL:
- phpMyAdmin: Web-based, feature-rich
- Sequel Ace: Native Mac app, user-friendly (free)
- TablePlus: Modern interface, supports multiple databases
Version Control
Don't forget to initialize a git repository:
git init
echo "config.php" > .gitignore # Never commit database credentials!
git add .
git commit -m "Initial commit: Task checklist application"
Common Issues and Solutions
Port Already in Use
If port 8000 is taken, use a different one:
php -S localhost:8080
MySQL Connection Refused
Make sure MySQL is running:
brew services list
brew services restart mysql
Permission Denied Errors
Check your MySQL user has proper permissions:
mysql -e "GRANT ALL PRIVILEGES ON task_checklist.* TO 'root'@'localhost';"
mysql -e "FLUSH PRIVILEGES;"
Taking It Further
Once you're comfortable with this basic application, consider these next steps:
- Add User Authentication: Let multiple users have their own task lists
- API Development: Create a REST API and separate frontend
- Use a Framework: Try Laravel (PHP) or build the same app with Node.js and Express
- Deploy to Production: Learn about hosting, domains, and SSL certificates
- Add Real-time Features: Use WebSockets for multi-user collaboration
Need Professional Development Help?
Building database-driven applications is just the beginning. If you're working on a complex project that requires:
- Custom database architecture and optimization
- Scalable cloud infrastructure
- AI/ML integration with your web applications
- Production-ready deployment and monitoring
- Team collaboration and DevOps pipelines
Falcon Internet specializes in building robust, scalable web applications with modern technology stacks. Our team has expertise in PHP, MySQL, cloud hosting, and AI-powered solutions.
Contact us today to discuss your project needs. Whether you need help architecting a new application, optimizing an existing one, or deploying to production infrastructure, we're here to help!
Conclusion
Congratulations! You've successfully set up a local PHP and MySQL development environment and built a complete database-driven application with Claude Code. You've learned about database connections, prepared statements, CRUD operations, and running a local development server.
The skills you've gained here form the foundation for building almost any web application. Keep experimenting, ask Claude Code questions when you're stuck, and most importantly—keep building!
In Part 3, we'll explore deploying your application to a production server and setting up a custom domain. Stay tuned!
Happy coding!