Download the PHP package r7di4am/rdb without Composer
On this page you can find all versions of the php package r7di4am/rdb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package rdb
Short Description A simple PDO wrapper class for database operations
License GPL-3.0
Rated 5.00 based on 1 reviews
Informations about the package rdb
Database Operations with PDO - Basic Operations
Project Overview
This project shows how to work with a database using PDO (PHP Data Objects) through a custom rdb
class. The main features include:
- Connecting to the database.
- Running basic queries like SELECT and INSERT.
- Handling database transactions (making sure everything works together correctly).
Features
- Run database queries with prepared statements.
- Execute multiple queries in one transaction to make sure all of them succeed or fail together.
- Get results in different formats (like arrays or objects).
- Simple interface for working with the database.
Requirements
- PHP 7.x or higher.
- MySQL or MariaDB database.
- A database table
users
with fieldsid
,username
, andpassword
for testing.
Install
- composer require r7di4am/rdb
- In PHP Use It Like This
Table Schema
Here is an example table that works with this project:
How to Use
1. Connect to the Database
To connect to the database, you can use the following code:
- Replace
"root"
,"password"
, and"test"
with your actual database username, password, and database name.
2. Overview of rdb
Class
The rdb
class is a simple helper that makes it easier to interact with the database. Here are the key methods you can use:
Methods
query(string $query, array $params = [])
: Runs an SQL query.result()
: Returns the result of the query (PDOStatement).affected_rows()
: Returns how many rows were affected by the last query.fetch_all()
: Gets all the results as an associative array.fetch_row()
: Gets a single row as a numbered array.fetch_assoc()
: Gets a single row as an associative array.fetch_object()
: Gets a single row as an object.beginTransaction()
: Starts a transaction.commit()
: Saves changes made during the transaction.rollback()
: Cancels changes if something goes wrong.
3. Running Queries
Example: Get All Users
Example: Get a User by ID
Example: Insert a New User
4. Using Transactions
Example: Running Multiple Queries Together (Transaction)
5. Error Handling
try-catch
Blocks: We usetry-catch
blocks to catch any errors that might happen during the database operations. This is helpful for handling issues like connection problems or invalid queries.
Conclusion
This project shows how to work with a database using PDO in PHP. The rdb
class makes it easier to run queries and handle transactions. It also allows you to fetch data in different formats and manage errors effectively.
Key features:
- Easy-to-use PDO-based database operations.
- Support for transactions (begin, commit, rollback).
- Methods for fetching data in different formats (array, object).
- Simple error handling.
Let me know if you need any help or have questions!
Made with ❤️ by @R7di4am