This is a sample PHP project demonstrating the use of PDO for database connections, CRUD operations with a User class, and unit testing with PHPUnit.
- PHP 7.4 or higher
- MySQL
- Composer
- MAMP or another local development environment
-
Clone the repository:
git clone https://github.com/ndaboom/sample-project.git cd sample-project -
Install dependencies:
composer install
-
Configure the database:
-
Start your MySQL server (MAMP or other).
-
Create a database named
test_db. -
Update the database connection details in
objects/Database.phpif necessary:private $host = "127.0.0.1"; private $db_name = "test_db"; private $username = "root"; private $password = "root";
-
-
Import the database schema:
CREATE TABLE users ( id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL );
- Place the project in your web server's root directory. For MAMP, place it in the
htdocsdirectory. - Access the project via your browser. For example,
http://localhost/sample-project.
-
Ensure the MySQL server is running.
-
Run the tests using PHPUnit:
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests
-
Connection error: SQLSTATE[HY000] [2002] No such file or directory
- Ensure the MySQL server is running.
- Check the host and socket settings in
objects/Database.php.
-
Call to a member function prepare() on null
- This indicates a failed database connection. Verify the connection details and ensure the server is accessible.
For any questions or issues, please contact [ndabosam084@gmail.com].