naukakodu/php-mysql-connection

There is no license information available for the latest version (1.1.1) of this package.

1.1.1 2025-05-23 16:13 UTC

This package is auto-updated.

Last update: 2025-06-06 16:31:28 UTC


README

composer require php-mysql-connection/database

Features

The library offers a simple Database class with the following capabilities:

  • Establishing a secure PDO connection with a MySQL database
  • Executing SQL queries with prepared statements support
  • Retrieving single rows or entire result sets
  • Handling data insertion with the ability to retrieve the last inserted ID

Usage Examples

Basic Connection

use Naukakodu\PhpMysqlConnection\Database;

try {
    $db = new Database('localhost', 'database_name', 'username', 'password');
    
    // ...
} catch (Exception $e) {
    echo $e->getMessage();
}

Retrieving Data

// Fetching all rows
$users = $db->fetchAll("SELECT * FROM users WHERE status = ?", ['active']);

// Fetching a single row
$user = $db->fetch("SELECT * FROM users WHERE id = ?", [1]);

Inserting Data

$userId = $db->insert(
    "INSERT INTO users (name, email, created_at) VALUES (?, ?, NOW())",
    ['John Smith', 'john@example.com']
);

echo "Added user with ID: " . $userId;

Executing Other Queries

$db->query("UPDATE users SET status = ? WHERE id = ?", ['inactive', 5]);

Accessing the PDO Object

$pdo = $db->getConnection();
// ...

Configuration

The default character set is utf8mb4. You can change it during initialization:

$db = new Database('localhost', 'database_name', 'username', 'password', 'utf8');