PHP code example of maggsweb / maggsweb-pdo

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

    

maggsweb / maggsweb-pdo example snippets


define('DBHOST', '');  //eg: 127.0.0.1
define('DBUSER', '');  //eg: root
define('DBNAME', '');  //eg: admin
define('DBPASS', '');  //eg: password

$db = new Maggsweb\MyPDO((DBHOST, DBUSER, DBNAME, DBPASS);

$sql = "INSERT INTO `names` VALUES 
            (NULL, 'Joe',  'Bloggs'),
            (NULL, 'John', 'Bloggs'),
            (NULL, 'Jane', 'Bloggs');";

$db->query($sql);

$result = $db->execute();

$sql = "SELECT * FROM `names`";

$db->query($sql);

$results = $db->fetchAll();           // Multiple rows, returned and an Object Array
$results = $db->fetchAll('Array');    // Multiple rows, returned as a multi-dimensional array

$sql = "SELECT * FROM `names` LIMIT 1";
$db->query($sql);

$result  = $db->fetchRow();           // Single row, returned as an Object
$result  = $db->fetchRow('Array');    // Single row, returned as an Array

$sql = "SELECT name FROM `names` LIMIT 1";
$db->query($sql);

$result  = $db->fetchOne();           // Single value, returned as a String

$sql = "SELECT * FROM `names` WHERE firstname = :firstname";

$db->query($sql);

$db->bind(':firstname', 'Chris');

$results = $db->fetchAll(); 

// or

$results = $db->query("SELECT * FROM `names` WHERE firstname = :firstname")
              ->bind(':firstname', 'Chris')
              ->fetchAll();


if ($results) {
    foreach ($results as $result) {
        echo $result->{$column};
    }
} else {
    echo $db->getError();
}

$table   = 'names';
$columns = ['firstname' => 'Fred', 'surname' => 'Bloggs'];

$result = $db->insert($table, $columns);

echo $result
    ? $db->numRows() . ' records affected'
    : $db->getError();


$id = $db->insertID();


$table   = 'names';
$columns = ['firstname' => 'Fred', 'surname' => 'Bloggs'];

$result = $db->update($table, $columns);

$table   = 'names';
$columns = ['firstname' => 'Fred 2', 'surname' => 'Bloggs 2';
$where   = "firstname = 'Fred' AND surname = 'Bloggs'";  //'WHERE' is not needed

$result = $db->update($table, $columns, $where);

$table   = 'names';
$columns = ['firstname' => 'Fred 2', 'surname' => 'Bloggs 2'];
$where   = ['firstname' => 'Fred',   'surname' => 'Bloggs'];

$result = $db->update($table,$columns,$where);

echo $result
    ? $db->numRows() . ' records affected'
    : $db->getError();

$table  = 'names';
$where  = "surname = 'Doe'";

$result = $db->delete($table, $where);

$table = 'names';
$where = ['surname' => 'Doe'];

$result = $db->delete($table, $where);

echo $result
    ? $db->numRows() . ' records affected'
    : $db->getError();