PDO Database Class ============================ A database class for PHP-MySQL which uses the PDO extension. ## Feature overview - Simple fetching - Logger class ## To use the class #### 1. Edit the database settings in the settings.ini.php ``` [SQL] host = localhost user = root password = dbname = yourdatabase ``` #### 2. Require the class in your project ```php query("SELECT * FROM persons"); ``` #### Fetching with Bindings: There are three different ways to bind parameters. ```php bind("id","1"); $db->bind("firstname","John"); $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id"); // 2. Bind more parameters $db->bindMore(array("firstname"=>"John","id"=>"1")); $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id")); // 3. Or just give the parameters to the method $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1")); ``` #### Fetching Row: This method always returns only 1 row. ```php row("SELECT * FROM Persons"); ``` #### Fetching Single Value: This method returns only one single value of a record. ```php bind("id","3"); $firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id"); echo $firstname; // Wicky ``` #### Fetching Column: ```php column("SELECT Firstname FROM Persons"); ``` #### Delete a record: ```php query("DELETE FROM Persons WHERE Id = :id", array("id"=>"1")); ``` #### Update a record: ```php query("UPDATE Persons SET firstname = :f WHERE Id = :id", array("f"=>"Jan","id"=>"32")); ``` #### Insert a record: ```php query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)", array("f"=>"Vivek","age"=>"20")); ``` 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. ## How to use easyCRUD #### 1. First, Create a new class. Then require the easyCRUD class. #### 2. Extend your class and add the following fields to the class. ```php Firstname = "Kona"; $person->Age = "20"; $person->Sex = "F"; $created = $person->Create(); // Or give the bindings to the constructor $person = new person(array("Firstname"=>"Kona","age"=>"20","sex"=>"F")); $created = person->Create(); // SQL Equivalent "INSERT INTO persons (Firstname,Age,Sex) VALUES ('Kona','20','F')" ``` #### Deleting a record ```php Id = "17"; $deleted = $person->Delete(); // Shorthand method, give id as parameter $deleted = $person->Delete(17); // SQL Equivalent "DELETE FROM persons WHERE Id = 17 LIMIT 1" ``` #### Saving person's data ```php Firstname = "Nhoj"; $person->Age = "20"; $person->Sex = "F"; $person->Id = "4"; $saved = $person->Save(); // Or give the bindings to the constructor $person = new person(array("Firstname"=>"John","age"=>"20","sex"=>"F","Id"=>"4")); $saved = $person->Save(); // SQL Equivalent "UPDATE persons SET Firstname = 'John',Age = 20, Sex = 'F' WHERE Id= 4" ``` #### Finding a person ```php Id = "1"; $person = $person->Find(); // Shorthand method, give id as parameter $person = $person->Find(1); // SQL Equivalent "SELECT * FROM persons WHERE Id= 1" ``` #### Getting all the persons ```php all(); // SQL Equivalent "SELECT * FROM persons ```