PHP code example of modularr / database

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

    

modularr / database example snippets


DB::connect($db='test',$pass='',$user='root',$host='localhost',$type='mysql');
DB::getPdo();
DB::setPdo($db);
DB::quote($string,$remove_quotes=false);
DB::query($query, $params = array());
DB::fetchAll($query);
DB::fetchAll_safe($query);
DB::fetch_assoc($query);
DB::fetch_safe_assoc($query);
DB::fetch_object($query);
DB::fetch_safe_object($query);
DB::num_rows($query);

$db = new Database($db='test',$pass='',$user='root',$host='localhost',$type='mysql'); # Default Syntax
$db = new Database(['host'=>$host,'dbname'=>$database,'user'=>$username,'pass'=>$password]); # Alternative Syntax

$db->connect(DB,PASS,USER,HOST); # Establish a Connection With PDO

$db->setPdo($pdo); # Assign PDO Connection to the Database Class

$db = new Database(DB,PASS,USER,HOST); # Establish a Connection
$query = $db->query("SELECT * FROM table");
while($item = $db->fetch_object($query))
{
    echo'#'.htmlspecialchars($item->id).': '.htmlspecialchars($item->name).'<br>';
}

$db = new Database(DB,PASS,USER,HOST); # Establish a Connection
DB::Facade($db); # Initiate Database object Facade

DB::connect('database','pass','user','host');

$query = DB::query("SELECT * FROM table");
while($item = DB::fetch_object($query))
{
    echo'#'.htmlspecialchars($item->id).': '.htmlspecialchars($item->name).'<br>';
}

$query = DB::query("SELECT * FROM table WHERE id = ?", [$_GET['id']]);

$quoted_string = DB::quote($_GET['id']);

# Remove Quotes after quoting, and right before output,
# giving you a similar string as mysql_real_escape_string
$quoted_string = DB::quote($_GET['id'], 1);

# Default Quote adds '' quotes around the field, forcing you to do:
DB::query("SELECT * FROM table WHERE field LIKE ?", ['%'.$input.'%']);
DB::query("SELECT * FROM table WHERE field LIKE ".DB::quote('%'.$input.'%'));

# Removed Quoting, quotes but removes added quotes
DB::query("SELECT * FROM table WHERE field LIKE '%".DB::quote($input,1)."%'";

$table = DB::fetch_object($query);

$table = DB::fetch_safe_object($query);

DB::num_rows($query); # Equivalent of $pdo->rowCount();

# Loop Objects
while($entry = DB::fetch_safe_object($query))
{
	# Because of fetch_safe_object we don't need to apply htmlspecialchars
    echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
}
# Single Object
$entry = DB::fetch_safe_object($query);
echo $entry->name;

# Loop Objects Using Foreach instead with Fetchall
foreach(DB::fetchAll_safe($query) as $entry)
{
	# Because of fetchAll_safe we don't need to apply htmlspecialchars
    echo '<a href="page?id='.$entry->id.'">'.$entry->name.'</a><br />';
}
# Single Object
$entry = DB::fetchAll_safe($query);
echo $entry[0]->name;