PHP code example of carry0987 / sanite

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

    

carry0987 / sanite example snippets


use carry0987\Sanite\Sanite;

// Database connection settings
$config = array(
    'host' => 'mariadb',
    'database' => 'dev_sanite',
    'username' => 'test_user',
    'password' => 'test1234',
    'port' => 3306, // Optional
    'charset' => 'utf8mb4' // Optional
);

// Create a database connection
$sanite = new Sanite($config);

namespace carry0987\Sanite\Example;

use carry0987\Sanite\Models\DataReadModel;

class UserModel extends DataReadModel
{
    // Implement your methods, for example:
    public function getUserById(int $userId)
    {
        $queryArray = [
            'query' => 'SELECT * FROM user WHERE uid = ? LIMIT 1',
            'bind'  => 'i',  // This value needs to be relative when using DBUtil::getPDOType
        ];
        $dataArray = [$userId];

        return $this->getSingleData($queryArray, $dataArray);
    }

    public function getAllUsers()
    {
        $queryArray = [
            'query' => 'SELECT * FROM user'
        ];

        return $this->getMultipleData($queryArray);
    }
}

use carry0987\Sanite\Example\UserModel;

// Instantiate UserModel
$userModel = new UserModel($sanite);

// Retrieve user information for user with ID 1
$user = $userModel->getUserById(1);
$users = $userModel->getAllUsers();

print_r($user);
print_r($users);

try {
    // ... attempt some database operations ...
} catch (\carry0987\Sanite\Exceptions\DatabaseException $e) {
    // ... handle database exception ...
    echo "Error: " . $e->getMessage();
}