PHP code example of naukakodu / php-mysql-connection

1. Go to this page and download the library: Download naukakodu/php-mysql-connection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

naukakodu / php-mysql-connection example snippets


use Naukakodu\PhpMysqlConnection\Database;

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

// 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]);

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

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

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

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

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

$database = new Database('localhost', 'database_name', 'username', 'password');
$queryBuilder = $database->getQueryBuilder();

$queryBuilder
    ->table('users')
    ->join('tasks', 'users.id', '=', 'tasks.user_id');

$queryBuilder
    ->table('users')
    ->limit(10);

$queryBuilder
    ->table('users')
    ->orderBy('name'); // ASC

$queryBuilder
    ->table('users')
    ->orderBy('name', 'desc'); // DESC

$queryBuilder
    ->table('users')
    ->where('name', 'John');

$queryBuilder
    ->table('users')
    ->where('name', 'John')
    ->execute(); // Returning the array of rows

$queryBuilder
    ->table('users')
    ->where('name', 'John')
    ->toSql();

composer