Skip to content

This package can prepare and execute SQL queries with MySQL. It can connect to a given MySQL database server using the MySQLi extension. The package can prepare and execute prepared queries passing an array of parameter values. It can return the query results one at a time or all at once in an array. A separate class can return the details of an…

Notifications You must be signed in to change notification settings

youngcet/DatabaseHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DatabaseHandler

This package prepares and executes MySQL Statements

Usage

require ('autoloader.php');

$dbhandler = new DatabaseHandler();

// prepare the statement (can be a SELECT/INSERT/UPDATE/DELETE)
$sql = $dbhandler->prepareStatement ('SELECT * FROM table WHERE id = ? AND email = ?');
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

// execute the statement
// 1st parameter is an array of values
// 2nd parameter is the binding string corresponding to the values in that order (i = integer, s = string, d = float, b = blob)
$sql = $dbhandler->executeStatement ([6, 'young.cet@gmail.com'], 'is');
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

// if the query does not require passing values or the WHERE clause (e.g. SELECT * FROM table), pass empty values to executeStatement
$sql = $dbhandler->executeStatement ([], '');


// fetch the results as a single row
$sql = $dbhandler->fetchRow();

// fetch all rows
$sql = $dbhandler->fetchAll();


####################################################################################################################

// LOADING MULTIPLE SQL QUERIES
$queries = 
    [
        'SELECT_ALL_USERS' => 'SELECT * FROM table', 
        'SELECT_USER' => 'SELECT * FROM table WHERE id = ?',
        'INSERT_USER' => 'INSERT INTO table (fname, lname, email) VALUES (?,?,?)'
    ];

$sql = $dbhandler->loadQueries ($queries);
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

$sql = $dbhandler->executeLoadedQuery ('SELECT_ALL_USERS', [], ''); // argument 2 & 3 are empty since this statement does not have a where clause
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

// // fetch the results
print_r($dbhandler->fetchAll());

$sql = $dbhandler->executeLoadedQuery ('SELECT_USER', [6], 'i');
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

// fetch the results
print_r($dbhandler->fetchRow());

$sql = $dbhandler->executeLoadedQuery ('INSERT_USER', ['yung', 'cet', 'young.cet@gmail.com'], 'sss');
if (App\Custom\Error::IsAnError ($sql))
{
    // handle error
    // $sql->GetError() // get error message
    // $sql->GetErrorCode() // get error code
}

// close the connection
$dbhandler->closeConnection();

Full documentation on the wiki: https://github.com/youngcet/DatabaseHandler/wiki

About

This package can prepare and execute SQL queries with MySQL. It can connect to a given MySQL database server using the MySQLi extension. The package can prepare and execute prepared queries passing an array of parameter values. It can return the query results one at a time or all at once in an array. A separate class can return the details of an…

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages