PHP Classes


Recommend this page to a friend!
  Classes of Vojin Petrovic   PHP Database Tables Class   Download  
Role: Documentation
Content type: text/plain
Description: Readme
Class: PHP Database Tables Class
Store and retrieve class objects in database table
Author: By
Last change: Changes in readme file according to changes in MySql class
Date: 1 year ago
Size: 2,400 bytes


Class file image Download
# PHP-Database-Tables-Class --------------------- OOP Database class for PHP MySQL connection and insert, update, delete and select query for easier manipulating with tables ### CONFIG File All database configurations are stored in seperate config.php file, which can be kept off document root. ### TABLE CLASSES Whole idea is to create separate class for each table in the database and to manipulate data with ease. e.g. **class Article extends MySqlHelper {** **public $id = 0;** **public $title = "";** **public function __construct(int $id = 0) {** **$this->table = "articles";** **$this->primaryKeyColumn = "id";** **$this->columns = ["id" => MySqlHelper::type_int,"title" => MySqlHelper::type_str];** **$this->loadRegular($id);** **}** **}** ### LOADING SINGLE ROW FROM A TABLE AND ASSIGN VALUES e.g: **$article = new Article(3);** this will load row with id 3 in article class and convert all values appropriately. $article->id and $article->title in this case will be set **$article->title = "New title";** //changing class value **$article->update();** //updating article class values to table or **$article->update(["title"]);** //updating just title value ### CUSTOM DATABASE CONNECTION AND QUERIES, IF NEEDED Database class loads configuration file and establishes a MySQLi connect in constructor so just creating an instance of class connects you with database. e.g: **$db = new MySql();** // would connect to database There are plenty of query types, reading whole tables, single rows, array of single column values, updating and deleting rows with or without where clause, counting row numbers, etc. e.g: **$db->insert("tablename",["column1","column2","column3"],[$value1,$value2,$value3]);** //inserting values in table **$db->update("tablename",["column1","column2"],[$newvalue1,$newvalue2],$id);** // updating two columns to row with primar key value $id **$db->updateWhere("tablename",["column1"],[$newvalue1],"id>?",[3]);** // updating column `column1` with value $newvalue1 to rows with `id` greater than 3 **$db->deleteWhere("tablename","id>?",[3]);** // deleting all rows with `id` greater than 3 Examples of Use: --------------- index.php has a sample loading & editing values from tables, as well as reading POST and GET variables and assigning values to database.