must immediately created before in use! $db = mysqli_connect("localhost", $user, $pw, $dbname); if(!$db){ $db_result = FALSE; return $db_result; } else { mysqli_set_charset($db, 'utf8'); return $db; } } // create a table function createTable($db, $tablename, $fields) { //dbname= string 'DB-Name'; fields= 'id' INT (10),'name' VARCHAR(64),'and so on... $sqlfields = 'CREATE TABLE `'.$tablename.'` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, '.$fields.');'; if (ifTableExist($db, $tablename) == FALSE) { $db_result = mysqli_query($db, $sqlfields) or die("Table create ERROR: ".mysqli_error($db)); return $db_result; } else return FALSE; } // add a new data entry function addData($db, $tablename, $field, $data) { //add dates like is declared in $data $newEntry = 'INSERT INTO `'.$tablename.'` ('.$field.') VALUES('.$data.');'; mysqli_query($db, $newEntry) or die("Data insert ERROR: ".mysqli_error($db)); return mysqli_insert_id($db); } // check if table exist function ifTableExist($db, $dbtable, $orderby=" ") { //asked if there data into a table -> if not, than result false or the dates, if something exist [if something->like order by] $ifTable = 'SELECT * FROM `'.$dbtable.'` '.$orderby.';'; $db_result = mysqli_query($db, $ifTable); if ($db_result) return $db_result; else return FALSE; } // create a new database function createDatabase($db, $dbname) { //create a database; imported is login to the database and its rights, value two is the new name of table $sqlname = 'CREATE DATABASE IF NOT EXISTS '.$dbname; $db_result = mysqli_query($db, $sqlname) or die("Database create ERROR: ".mysqli_error($db)); return $db_result; } // ask for data function queryFromTable($db, $dbtable, $orderby, $start=0,$length=1) { // asked for data in the database [options are order, start or length] $db_result = ifTableExist($db, $dbtable, $orderby); if ($db_result == FALSE) return FALSE; //break, because no data everywhere $data = array(); while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } // search an entry function searchEntry($db,$table,$field,$value, $limit="1") { //search a entry into the database like the mentioned in field; common used is by LIMIT=one $sqlsearch = "SELECT * FROM $table WHERE $field = '$value' LIMIT $limit"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // search entries like order by function searchEntriesOrderBy($db,$table,$field,$value,$orderby) { //search a entry into the database like the mentioned in field; $sqlsearch = "SELECT * FROM $table WHERE $field = '$value' ORDER BY $orderby"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // exchange data into database function updateData($db, $tablename, $id, $field, $data) { // UPDATE FFWParty SET BookedAdult=20 WHERE id=1 $updateEntry = "UPDATE $tablename SET $field = '$data' WHERE id= '$id'"; $db_result = mysqli_query($db, $updateEntry) or die("Data update ERROR: ".mysqli_error($db)); return $db_result; } // search entry like function searchEntryLike($db,$table,$field,$value) { //search entries into the database by use like operator $sqlsearch = "SELECT * FROM $table WHERE $field LIKE '$value'"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // get entries and usual by limit one function getEntries($db,$table,$limit="1") { //get the first entry $sqlsearch = "SELECT * FROM $table LIMIT $limit"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // get the last entry of table function getLastEntry($db,$table) { //get the last entry $sqlsearch = "SELECT * FROM $table ORDER BY ID DESC LIMIT 1"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // search entries between value one and two function searchEntryBetween($db,$table,$field,$value1,$value2) { //search between entries into the database by use like operator $sqlsearch = "SELECT * FROM $table WHERE $field BETWEEN '$value1' AND '$value2'"; $data = array(); $db_result = mysqli_query($db, $sqlsearch); if ($db_result->num_rows > 0) { while ($obj=mysqli_fetch_object($db_result)) { //fetch the contents array_push($data, $obj); } return $data; } else return FALSE; } // lock table for unique change function lockTable($db, $table, $action="UNLOCK") { // table lock or unlock for exclusive open if($action == "UNLOCK") $sqlLock = "UNLOCK TABLE"; else $sqlLock = "LOCK TABLE $table WRITE"; $db_result = mysqli_query($db, $sqlLock) or die("Table LOCK ERROR: ".mysqli_error($db)); return $db_result; } // del an entry by ID function deleteEntryById($db,$table,$id) { // delete a entry for ever by ID (ID is the singular property of one entry into the database and table) $sqldelete = "DELETE FROM $table WHERE id=$id LIMIT 1"; // delete one entry only, by order "LIMIT 1" $db_result = mysqli_query($db, $sqldelete) or die("Data delete ERROR: ".mysqli_error($db)); if ($db_result) return TRUE; else return FALSE; } // close database function closeDatabase($db) { // simply close every questions at the database mysqli_close($db); } ?>