Download the PHP package sectoroverload2k/php-mysql-database without Composer
On this page you can find all versions of the php package sectoroverload2k/php-mysql-database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sectoroverload2k/php-mysql-database
More information about sectoroverload2k/php-mysql-database
Files in sectoroverload2k/php-mysql-database
Package php-mysql-database
Short Description Lightweight PHP MySQL wrapper with prepared statements. Query returns MySQL object - call fetchAssoc() or fetchAll() to get data.
License MIT
Homepage https://github.com/sectoroverload2k/php-crud
Informations about the package php-mysql-database
php-mysql-database
A lightweight PHP MySQL database wrapper with prepared statement support and automatic parameter binding.
Installation
Quick Start
⚠️ Important: Understanding Return Types
KEY CONCEPT: All query methods return a MySQL object, NOT an array. You must call fetch methods to retrieve data.
Two Ways to Query
Method 1: Simple Query (Recommended)
Pass parameters directly to query() - most concise and preferred method:
Method 2: Prepare/Bind/Execute (Advanced)
Use when you need more control or plan to execute the same query multiple times:
Complete Examples
Select Single Row
Select Multiple Rows
Insert with Auto-Increment ID
Update Records
Delete Records
Complex Query with Multiple Parameters
Join Query
Fetch Methods
After executing a SELECT query, use these methods to retrieve data:
Parameter Type Detection
Parameters are automatically typed based on PHP variable type:
Explicit Type Definition (Optional)
You can manually specify types if needed:
Type identifiers:
i- Integerd- Double (floating-point)s- Stringb- Blob (binary data)
Error Handling
The library throws exceptions on database errors:
Common Patterns
Check if Record Exists
Count Records
Pagination
Conditional Queries
Transaction Support
Wrap multiple queries in a transaction to ensure atomicity - either all operations succeed or all fail.
Basic Transaction
Money Transfer Example
Transaction Methods
beginTransaction(): bool
Start a new transaction.
Returns: True on success Throws: DBErrorException on failure
commit(): bool
Commit the current transaction.
Returns: True on success Throws: DBErrorException on failure or if no transaction is active
rollback(): bool
Rollback the current transaction.
Returns: True on success Throws: DBErrorException on failure or if no transaction is active
inTransaction(): bool
Check if currently in a transaction.
Returns: True if in transaction, false otherwise
Transaction Best Practices
- Always use try-catch - Catch exceptions and rollback on errors
- Commit explicitly - Don't rely on auto-commit
- Keep transactions short - Minimize lock time on tables
- Handle all errors - Always rollback on any exception
- Check state when needed - Use
inTransaction()if transaction state is uncertain
API Reference
Database Class
__construct(array $config)
Creates database connection.
Config array:
server- Database hostusername- Database userpassword- Database passworddatabase- Database name
query(string $sql, array $params = []): MySQL|null
Execute a query with optional parameters.
Returns: MySQL object on success, null on failure Throws: DBErrorException, ForeignKeyException
prepare(string $sql): MySQL|null
Prepare a statement for execution.
Returns: MySQL object on success, null on failure
bind_param(MySQL $stmt, mixed $params, string $types = null): MySQL
Bind parameters to prepared statement.
Returns: MySQL object
execute(MySQL $stmt): MySQL
Execute a prepared statement.
Returns: MySQL object with results
insert_id(): int
Get the last inserted auto-increment ID.
Returns: Integer ID
affected_rows(): int
Get the number of affected rows from the last query.
Returns: Number of rows affected by the last INSERT, UPDATE, DELETE, or REPLACE query
beginTransaction(): bool
Start a database transaction.
Returns: True on success Throws: DBErrorException
commit(): bool
Commit the current transaction.
Returns: True on success Throws: DBErrorException
rollback(): bool
Rollback the current transaction.
Returns: True on success Throws: DBErrorException
inTransaction(): bool
Check if currently in a transaction.
Returns: Boolean transaction state
MySQL Class (Result Object)
fetchAssoc(): array|null
Fetch single row as associative array.
Returns: Array on success, null if no more rows
fetchAll(): array
Fetch all remaining rows as array of associative arrays.
Returns: Array of arrays (empty array if no rows)
fetchRow(): array|null
Fetch single row as numeric array.
Returns: Array on success, null if no more rows
numRows(): int
Get number of rows in result set.
Returns: Integer count
numFields(): int
Get number of columns in result set.
Returns: Integer count
Best Practices
- Always use parameterized queries - Never concatenate user input into SQL
- Always fetch results - Call
fetchAssoc()orfetchAll()on SELECT queries - Check for null -
fetchAssoc()returns null when no rows found - Use try-catch - Catch database exceptions in production
- Close connections - Call
$db->disconnect()when done (optional, happens automatically)
Common Mistakes to Avoid
❌ Don't use result object as array:
✅ Do fetch the data first:
❌ Don't forget parameters for INSERT/UPDATE/DELETE:
✅ INSERT/UPDATE/DELETE don't return data:
License
MIT License
Contributing
Pull requests are welcome. For major changes, please open an issue first.
Support
For issues and questions, please use the GitHub issue tracker.