PHP code example of tnapf / pdo

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

    

tnapf / pdo example snippets


$driver = (new Driver)
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->withPort(3306)
	->withPrefix(Driver::PREFIX_MYSQL)
	->connect()
;

$driver = Driver::createMySqlDriver("root", "password", "database")->connect();
$driver = Driver::createPostgresSqlDriver("root", "password", "database")->connect();
$driver = Driver::createSQLiteDriver("/path/to/db.sqlite")->connect();

// you can set the host and port in the last two parameters but they default to localhost and the default port of the service

$rows = $driver->query("SELECT * FROM table")->fetchAll(PDO::FETCH_ASSOC);

$driver->prepare("SELECT * FROM table WHERE column = :column");
$driver->bindValue("column", "value");
$driver->execute();
$rows = $driver->fetchAll(PDO::FETCH_ASSOC);

(new Driver(true))
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->connect()
;

function getRowWhereIdIs(int $id, int $fetch_type = PDO::FETCH_ASSOC): mixed
{
	Driver::prepare("SELECT * FROM table WHERE id = :id");
	Driver::bindValue("id", $id);
	Driver::execute();
	return Driver::fetch($fetch_type);
}

$row = getRowWhereIdIs(20);