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.
- Extract PHP files in your www directory.
- Install MySQL database server if not installed
require('db.php');
Db::config('yourDatabasename','yourUsername','yourPassword');
Db::getDb();
$sql = "SELECT * FROM yourTablename";
$results = Db::getDb()->query($sql);
foreach($results as $result)
{
echo $result['yourColumnName'];
}
$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'];
}
$sql = "INSERT INTO yourTablename(id,name) VALUES(:id,:name)";
$userInputId = 'yourUnescapedValue';
$userInputName = 'yourUnescapedValue';
$preparedStatements = array(':id'=>$userInputId,':name'=>$userInputName);
Db::getDb()->query($sql,$preparedStatements);
$sql = "INSERT INTO yourTablename(name) VALUES(:name)";
$userInputName = 'yourUnescapedValue';
$preparedStatements = array(':name'=>$userInputName);
$id = Db::getDb()->queryAndReturnId($sql,$preparedStatements);
Licensed under The MIT License (MIT).