DatBazo(Datuma Bazo, Database in Esperanto) is a SQL-query constructor using PDO. is a small library with which you can quickly create queries to the database using the "Prepare" and "Execute" methods of PDO to avoid sql injections.
This package requires PHP 7 or higher.
composer require kroyxlab/datbazo
Copy the direct repository to your project and require the class.
require_once 'proyect_directory/datbazo/src/DatBazo.php';
use kroyxlab\datbazo\DatBazo as DatBazo;
Modify the KLPdo.ini file located in the folder vendor/kroyxlab/datbazo/src/DBconfig.ini
and modify the values to configure the connection to the database.
[databazo]
db_driver = Mysql || sqlite3 || pgsql
db_host = Host_name
db_port = Port
db_name = Database_name
db_user = user
db_password = password
db_charset = UTF8
If everything is configured correctly, you can start using the library.
require_once "vendor/autoload.php";
use kroyxlab\datbazo\DatBazo as DatBazo;
// instantiate the DatBazo class
$productos = new DatBazo;
// Create an SQL statement using the methods of the KLPdo class
$products->select(['products'=>'name, price'])
->where(['price'=>['>=', 12.5]])
->order('price')
->execute();
// Set the type of fetch you want.
$products->fetch('assoc');
// Use the Method -> render (); to output and format the result of the sql query
$products->render(function($product){
return "<p>The name of the product is {$product['name']} and the price is {$product['price']}</p>";
});
// Or use a foreach loop using the fetch method
foreach($products->fecth('obj') as $product){
echo "<p>The name of the product is $product->name and the price is $product->price</p>"
}
The methods of the DatBazo class help to create an SQL statement which will be executed through the prepare
and execute
methods of Pdo to avoid SQL injections.
- Select()
- join()
- Insert()
- Where()
- Update()
- Delete()
- Limit()
- Offset()
- Group()
- Order()
- Execute()
- Fetch()
- Render()
This project is licensed under the MIT License - see the MIT.md file for details