PHP code example of davidlienhard / database

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

    

davidlienhard / database example snippets


 declare(strict_types=1);
use DavidLienhard\Database\Exception as DatabaseException;
use DavidLienhard\Database\Mysqli;

try {
    $db = new Mysqli;
    $db->connect("hostname", "username", "password", "dbname");
} catch (DatabaseException $e) {
    echo "unable to connect to the database host";
    exit(1);
}

 declare(strict_types=1);
use DavidLienhard\Database\Mysqli;

$userResult = $db->query(
    "SELECT
        `userID`,
        `userName`
    FROM
        `user`"
);

while ($userData = $userResult->fetch_assoc()) {
    echo $userData['userID'].": ".$userData['userName'].PHP_EOL;
}

 declare(strict_types=1);
use DavidLienhard\Database\Mysqli;
use DavidLienhard\Database\Parameter as DBParam;

$userResult = $db->query(
    "SELECT
        `userID`,
        `userName`
    FROM
        `user`
    WHERE
        `userLevel` = ? and
        `userType` = ?",
    new DBParam("i", $userLevel),
    new DBParam("s", $userType)
);

while ($userData = $userResult->fetch_assoc()) {
    echo $userData['userID'].": ".$userData['userName'].PHP_EOL;
}

 declare(strict_types=1);
use DavidLienhard\Database\Exception as DatabaseException;
use DavidLienhard\Database\Mysqli;
use DavidLienhard\Database\Parameter as DBParam;

try {
    $db->query(
        "INSERT INTO
            `user`
        SET
            `userName` = ?,
            `userLevel` = ?,
            `userType` = ?",
        new DBParam("s", $userName),
        new DBParam("i", $userLevel),
        new DBParam("s", $userType)
    );
} catch (DatabaseException $e) {
    echo "unable to update table";
    exit(1);
}