PHP code example of jpi / database

1. Go to this page and download the library: Download jpi/database 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/ */

    

jpi / database example snippets


$connection = new \JPI\Database(
    "mysql:host=localhost;dbname=your_database",
    "username",
    "password"
);

// Prepare a statement with bound parameters (without executing)
$statement = $connection->prep(
    "SELECT * FROM users WHERE email = :email;",
    ["email" => "[email protected]"]
);

// You can now execute it later
$statement->execute();

// Prepare and execute a query in one step
$statement = $connection->run(
    "SELECT * FROM users WHERE email = :email;",
    ["email" => "[email protected]"]
);

// Fetch results from the statement
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

$rows = $connection->selectAll("SELECT * FROM users;");

/**
$rows = [
    [
        "id" => 1,
        "first_name" => "Jahidul",
        "last_name" => "Islam",
        "email" => "[email protected]",
        "password" => "password123",
        ...
    ],
    [
        "id" => 2,
        "first_name" => "Test",
        "last_name" => "Example",
        "email" => "[email protected]",
        "password" => "password123",
        ...
    ],
    ...
];
*/

$row = $connection->selectFirst("SELECT * FROM users LIMIT 1;");

/**
$row = [
    "id" => 1,
    "first_name" => "Jahidul",
    "last_name" => "Islam",
    "email" => "[email protected]",
    "password" => "password",
    ...
];
*/

// INSERT
$numberOfRowsAffected = $connection->exec(
    "INSERT INTO users (first_name, last_name, email, password) VALUES (:first_name, :last_name, :email, :password);",
    [
        "first_name" => "Jahidul",
        "last_name" => "Islam",
        "email" => "[email protected]",
        "password" => "password",
    ]
);

// UPDATE
$numberOfRowsAffected = $connection->exec(
    "UPDATE users SET first_name = :first_name WHERE id = :id;",
    [
        "id" => 1,
        "first_name" => "Pabel",
    ]
);

// DELETE
$numberOfRowsAffected = $connection->exec("DELETE FROM users WHERE id = :id;", ["id" => 1]);

// INSERT a new user
$connection->exec(
    "INSERT INTO users (first_name, last_name, email, password) VALUES (:first_name, :last_name, :email, :password);",
    [
        "first_name" => "Jahidul",
        "last_name" => "Islam",
        "email" => "[email protected]",
        "password" => "password",
    ]
);

// Get the ID of the newly inserted row
$newRowId = $connection->getLastInsertedId();
// $newUserId = 3