Download the PHP package indieteq/indieteq-php-my-sql-pdo-database-class without Composer
On this page you can find all versions of the php package indieteq/indieteq-php-my-sql-pdo-database-class. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package indieteq-php-my-sql-pdo-database-class
I am not maintaining this repository anymore and I would like to request you to find a better repo or make a fork of this repository!
PDO Database Class
A database class for PHP-MySQL which uses the PDO extension.
To use the class
1. Edit the database settings in the settings.ini.php
Note if PDO is loading slow change localhost to -> 127.0.0.1 !
2. Require the class in your project
3. Create the instance
4. Logs - Modify the read/write rights of the root folder
Everytime an exception is thrown by the database class a log file gets created or modified. These logs are stored in the logs directory. Which means the database class needs write access for the logs folder. If the files are on a webserver you'll have to modify the rights of the root folder otherwise you'll get a "Permission denied" error.
The log file is a simple plain text file with the current date('year-month-day') as filename.
Examples
Below some examples of the basic functions of the database class. I've included a SQL dump so you can easily test the database class functions.
The persons table
id | firstname | lastname | sex | age |
---|---|---|---|---|
1 | John | Doe | M | 19 |
2 | Bob | Black | M | 41 |
3 | Zoe | Chan | F | 20 |
4 | Kona | Khan | M | 14 |
5 | Kader | Khan | M | 56 |
Fetching everything from the table
Fetching with Bindings (ANTI-SQL-INJECTION):
Binding parameters is the best way to prevent SQL injection. The class prepares your SQL query and binds the parameters afterwards.
There are three different ways to bind parameters.
More about SQL injection prevention : http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php
Fetching Row:
This method always returns only 1 row.
Result
id | firstname | lastname | sex | age |
---|---|---|---|---|
1 | John | Doe | M | 19 |
Fetching Single Value:
This method returns only one single value of a record.
Result
firstname |
---|
Zoe |
Using the like keyword
Result
id | firstname | lastname | sex | age |
---|---|---|---|---|
4 | Sekito | Khan | M | 19 |
Fetching Column:
Result
firstname |
---|
John |
Bob |
Zoe |
Kona |
Kader |
Delete / Update / Insert
When executing the delete, update, or insert statement by using the query method the affected rows will be returned.
Method parameters
Every method which executes a query has the optional parameter called bindings.
The row and the query method have a third optional parameter which is the fetch style. The default fetch style is PDO::FETCH_ASSOC which returns an associative array.
Here an example :
More info about the PDO fetchstyle : http://php.net/manual/en/pdostatement.fetch.php
EasyCRUD
The easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database. It uses the database class I've created to execute the SQL queries.
Actually it's just a little ORM class.