Posts

Showing posts from January, 2024

Creating a Simple Library REST API with PHP

  Step 1: Project Setup Begin by creating a project folder and navigating into it: mkdir library_api cd library_api Step 2: File Structure Create three files in your project folder: index.php: The main file for handling API requests. database.php: File for database connection. books.php: File for our simple "Book" model. Step 3: Database Connection In database.php, establish a connection to a MySQL database. Replace your_database_name, your_username, and your_password with your actual database credentials. Step 4: Book Model In books.php, define a simple Book model with properties for a book: <?php class Book {     public $id;     public $title;     public $author;     public $genre;     public function __construct($id, $title, $author, $genre) {         $this->id = $id;         $this->title = $title;         $this->author = $author;         ...

Building a Database with PHP, PDO, and MySQL on Linux

 Step 1: Set Up Database   Assuming you have PHP, Apache, and MySQL already installed (if not, refer to the previous  blog post), let’s create a new database and table .  In the MySQL shell, create a new database named " library_db ". CREATE DATABASE library_db;  Switch to the newly created database and  create a table called 'books' to store our data: USE library_db; CREATE TABLE books (     id INT AUTO_INCREMENT PRIMARY KEY,     title VARCHAR(255) NOT NULL,     author VARCHAR(255) NOT NULL,     genre VARCHAR(100) NOT NULL,     publication_year INT NOT NULL );  Step 2: PHP Setup for Database Connection Create a PHP file named crud_operations.php to establish a connection to the MySQL database using PDO. <?php $dsn = "mysql:host=localhost;dbname=library_db"; $username = "your_username"; $password = "your_password"; try {     $pdo = new PDO($dsn, $username, $password);   ...