PHP code example of sentgine / db

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

    

sentgine / db example snippets




use Sentgine\Db\QueryBuilder;

// Create a new QueryBuilder instance
$queryBuilder = new QueryBuilder();

// Select all columns from the 'users' table
$queryBuilder->select('users')->get();

// Select specific columns from the 'users' table
$queryBuilder->select('users', ['id', 'name', 'email'])->get();

// Select users where 'id' is equal to 1
$queryBuilder->select('users')->where('id', 1)->get();

// Select users where 'age' is greater than or equal to 18
$queryBuilder->select('users')->where('age', 18, '>=');

// Equivalent to SQL: SELECT * FROM users WHERE age >= 18

// Select users where 'points' are less than or equal to 100
$queryBuilder->select('users')->where('points', 100, '<=');

// Equivalent to SQL: SELECT * FROM users WHERE points <= 100

// Select users where 'salary' is greater than 50000
$queryBuilder->select('users')->where('salary', 50000, '>');

// Equivalent to SQL: SELECT * FROM users WHERE salary > 50000

// Select users where 'rating' is less than 4.5
$queryBuilder->select('users')->where('rating', 4.5, '<');

// Equivalent to SQL: SELECT * FROM users WHERE rating < 4.5

// Select users where 'id' is equal to 1 and 'status' is 'active'
$queryBuilder->select('users')->where('id', 1)->andWhere('status', 'active')->get();

// Select users where 'id' is equal to 1 or 'status' is 'inactive'
$queryBuilder->select('users')->where('id', 1)->orWhere('status', 'inactive')->get();

// Select only 10 users
$queryBuilder->select('users')->limit(10)->get();

// Select users with pagination, skipping the first 10 records
$queryBuilder->select('users')->limit(10)->offset(10)->get();

// Select users along with their corresponding posts
$queryBuilder->select('users')
    ->join('INNER', 'posts', 'users.id = posts.user_id')
    ->get();

// Insert a new user into the 'users' table
$queryBuilder->insert('users', [
    'name' => 'John Doe', 
    'email' => '[email protected]'
]);

// Update user with id 1
$queryBuilder->update('users', 
    ['name' => 'Jane Doe'], 
    ['id' => 1]
);

// Delete users with id greater than 10
$queryBuilder->delete('users', [
    'id' => [10, 20]
], '>');

// Execute a raw SQL query
$queryBuilder->raw('SELECT * FROM users WHERE id = 1')->get();

// Truncate the 'users' table
$queryBuilder->truncate('users');

use Sentgine\Db\QueryBuilder;

// Instantiate the QueryBuilder class
$queryBuilder = new QueryBuilder();

// Set the number of items per page
$perPage = 10;

// Get the current page number from the request, default to 1 if not provided
$current_page = $_GET['page'] ?? 1;

// Perform select statement
$queryBuilder->select('users');
$queryBuilder->where('age', 30, '>');


// Paginate the query results
$paginationData = $queryBuilder->paginate($perPage, $current_page);

// Retrieve the paginated data and pagination information
$data = $paginationData['data'];
$pagination = $paginationData['pagination'];

// Display the paginated data
foreach ($data as $row) {
    // Output each row of data
    echo $row->id . ' - ' . $row->name . '<br>';
}

// Display pagination links
echo '<br>Pagination:';
for ($page = 1; $page <= $pagination['total_pages']; $page++) {
    // Output pagination links
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
}

$db->select('targetTable1', array("*"));
$db->nestWhereExpression(' 
    ( 
        {{nest1}}    
        OR
        {{nest2}}    
    )
    AND
    (  {{nest3}}    )
');

$db->where('target_colum1', 235,"=", expression: 'nest1');
$db->orWhere('target_colum1', 543,"=", expression: 'nest1'); // same area as nest1 with OR operator
$db->orWhere('target_colum1', 111,"=", expression: 'nest1'); // same area as nest1 with OR operator

$db->where('target_colum1', 111,"=", expression: 'nest2');

$db->where('target_colum1', 111,"=", expression: 'nest3');
$db->andWhere('target_colum1', 111,"=", expression: 'nest3'); // same area as nest3 with AND operator
$sql = $db->buildSQL();; // Build the SQL query
$db->raw($sql)->execute(); // Execute the query