Skip to content

Latest commit

 

History

History
50 lines (48 loc) · 1.55 KB

README.md

File metadata and controls

50 lines (48 loc) · 1.55 KB

The only PDO MySQL class you need. Configured to support prepared and unprepared queries and to return the id of a newly created row if necessary. Creates the database if not exists.

Installation

  1. Extract PHP files in your www directory.
  2. Install MySQL database server if not installed

Usage

Configure your database

require('db.php');
Db::config('yourDatabasename','yourUsername','yourPassword');
Db::getDb();

Select a table

$sql = "SELECT * FROM yourTablename"; 
$results = Db::getDb()->query($sql);
foreach($results as $result)
{
    echo $result['yourColumnName'];
}

Select a table with prepared statements

$sql = "SELECT * FROM yourTablename WHERE id = :id";
$userInputValue = 'yourUnescapedValue';
$preparedStatements = array(':id'=>$userInputValue);
$results = Db::getDb()->query($sql,$preparedStatements);
foreach($results as $result)
{
    echo $result['yourColumnName'];
}

Insert a table with prepared statements

$sql = "INSERT INTO yourTablename(id,name) VALUES(:id,:name)";
$userInputId = 'yourUnescapedValue';
$userInputName = 'yourUnescapedValue';
$preparedStatements = array(':id'=>$userInputId,':name'=>$userInputName);
Db::getDb()->query($sql,$preparedStatements);

Insert a table with prepared statements and get the id

$sql = "INSERT INTO yourTablename(name) VALUES(:name)";
$userInputName = 'yourUnescapedValue';
$preparedStatements = array(':name'=>$userInputName);
$id = Db::getDb()->queryAndReturnId($sql,$preparedStatements);

License

Licensed under The MIT License (MIT).