PHP code example of giovanniramos / pdo4you

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

    

giovanniramos / pdo4you example snippets



/**
 * Loads the autoloader and all project dependencies
 * The data access have been defined in the 'initial configuration file'
 */


// Loading all the necessary files
DO4You\PDO4You;


/**
 * Main ways to start a connection instance
 */

// Connection instance started and available

# DEFAULT 
PDO4You::getInstance();


// Connecting to other data sources through a DSN

# MySQL / MariaDB
PDO4You::getInstance('instance_name', 'mysql:host=localhost;dbname=pdo4you;port=3306', 'user', 'pass');

# PostgreSQL
PDO4You::getInstance('instance_name', 'pgsql:host=localhost;dbname=pdo4you;port=5432', 'user', 'pass');

# CUBRID
PDO4You::getInstance('instance_name', 'cubrid:host=localhost;dbname=pdo4you;port=33000', 'user', 'pass');



// Loading all the necessary files
DO4You\PDO4You;

// Starting a connection instance. The default connection is not persistent
PDO4You::getInstance();

// Defining a persistent communication with the database
PDO4You::setPersistent(true);

// Selecting records in the database
PDO4You::select('SELECT * FROM books LIMIT 2');

// Selecting records and setting that connection instance will be used
PDO4You::select('SELECT * FROM books LIMIT 2', 'instance_name');


// Query statement
$sql = 'SELECT * FROM books LIMIT 2';

// Selecting records with PDO::FETCH_ASSOC
$result = PDO4You::select($sql);

// Selecting records with PDO::FETCH_NUM
$result = PDO4You::selectNum($sql);

// Selecting records with PDO::FETCH_OBJ
$result = PDO4You::selectObj($sql);

// Selecting records with PDO::FETCH_BOTH
$result = PDO4You::selectAll($sql);


// Selecting all records
$result = PDO4You::select('SELECT * FROM books');

// Getting the total number of rows affected by the operation
$total = PDO4You::rowCount();

// Displaying the query results
echo '<pre><h3>Query Result:</h3> ' , print_r($result, true) , '</pre>';



// SQL insert in JSON format
$json = '
	insert : [
		{
			table: "users" ,
			values: { mail: "[email protected]" }
		}
	] 
';

// The $result variable stores as return of the method, an array with the number of rows affected by the insert operation
$result = PDO4You::execute($json);

// Just after insertion, use the method PDO4You::lastId() to get the ID of the last insert operation in the database
$lastInsertId = PDO4You::lastId();

// If needed, enter the name of the sequence variable, 


// SQL insert in JSON format
$json = '
	insert : [
		{
			table: "users" ,
			values: { mail: "[email protected]" }
		},{
			table: "users" ,
			values: { mail: "[email protected]" }
		},{
			table: "books" ,
			values: { title: "title", author: "author" }
		}
	] 
';

// The $result variable stores an array with the number of rows affected by the insert operation
$result = PDO4You::execute($json);



// SQL update in JSON format
$json = '
	update : [
		{
			table: "users" ,
			values: { mail: "[email protected]" } ,
			where: { id: 2 }
		},{
			table: "users" ,
			values: { mail: "[email protected]" } ,
			where: { id: 3 }
		},{
			table: "books" ,
			values: { title: "new-title", author: "new-author" } ,
			where: { id: 1 }
		}
	] 
';

// The $result variable stores an array with the number of rows affected by the update operation
$result = PDO4You::execute($json);



// SQL delete in JSON format
$json = '
	delete : [
		{
			table: "users" ,
			where: { id: 2 }
		},{
			table: "users" ,
			where: { id: 5 }
		},{
			table: "users" ,
			where: { id: 10 }
		},{
			table: "books" ,
			where: { id: 10 }
		}
	] 
';

// The $result variable stores an array with the number of rows affected by the delete operation
$result = PDO4You::execute($json);



// The method below shows all the drivers installed and that are supported by the server
PDO4You::showAvailableDrivers();