PHP Classes

How to Use Prepared Statements in PHP to Execute a MySQL Query with Database Handler Package: Prepare and execute SQL queries with MySQL

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-10 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 65 All time: 10,162 This week: 157Up
Version License PHP version Categories
databasehandler 1.1The PHP License5PHP 5, Databases
Description Author

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 any errors that may happen during the preparation and execution of the queries.

Picture of Cedric Maenetja
  Performance   Level  
Name: Cedric Maenetja <contact>
Classes: 3 packages by
Country: South Africa South Africa
Innovation award
Innovation award
Nominee: 1x

Winner: 1x

Details

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

  Files folder image Files  
File Role Description
Files folder imageMySQL (3 files)
Accessible without login Plain text file autoloader.php Aux. Auxiliary script
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  MySQL  
File Role Description
  Plain text file Database.php Class Class source
  Plain text file DatabaseHandler.php Class Class source
  Plain text file Error.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:65
This week:0
All time:10,162
This week:157Up