php-crud/template_database.php

177 lines
6.0 KiB
PHP
Raw Normal View History

2020-01-02 23:21:46 +07:00
<?php
class Database
{ private $db_host = 'localhost'; //Database Host
private $db_user = 'root'; // Database User
private $db_pass = ''; // Database password
private $db_name = 'rai_crud'; //Database name
private $connection = false; // Checks to see if the connection is active
protected $result = array(); // Results returned from the query
public function connect(){
if(!$this->connection){ //checks if there is already a mysql connection active
$mycon = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); //connecting to my mysql
if($mycon){
$selectdb = @mysql_select_db($this->db_name,$mycon);
if($selectdb){
$this->connection = true; //sets the value to variable connection to true
return true;
}else{
echo "Tidak Menemukan Database";
return false;
}
}else{
echo "Tidak bisa tersambung ke mysql";
return false;
}
}else{
return true;
}
}
public function disconnect(){
if($this->connection){
if(@mysql_close()){
$this->connection = false;
return true;
}else{
return false;
}
}
}
public function select($table, $rows = '*', $where = null, $limit = null, $order = null){
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if($order != null) $q .= ' ORDER BY '.$order;
if($limit != null) $q .= ' LIMIT '.$limit;
//echo $q;
if($this->tableExists($table)){
$query = @mysql_query($q);
if($query){
$this->numRows = mysql_num_rows($query); //retrieves the number of rows from the query
for($i = 0; $i < $this->numRows; $i++)
{
$r = mysql_fetch_array($query); //mysql_fetch_array returns an array that corresponds to the fetched row
$key = array_keys($r); //array_keys() returns the keys, numeric and string, from the input array.
for($x = 0; $x < count($key); $x++) //count() counts all elements in an array
{
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x]))
{
if(mysql_num_rows($query) > 1)
$this->result[$i][$key[$x]] = $r[$key[$x]];
else if(mysql_num_rows($query) < 1)
$this->result = null;
else
$this->result[$key[$x]] = $r[$key[$x]];
}
}
}
return true;
//return $query;
}else{
echo "Ada kesalahan pada query";
return false;
}
}else {
echo "Table tidak ditemukan";
return false;
}
}
public function insert($table,$values,$rows = null) {
if($this->tableExists($table)){
$insert = 'INSERT INTO '.$table;
if($rows != null)$insert .= ' ('.$rows.')';
for($i = 0; $i < count($values); $i++){
if(is_string($values[$i]))$values[$i] = '"'.$values[$i].'"';
}
$values = implode(',',$values);
$insert .= ' VALUES ('.$values.')';
$insert_query = @mysql_query($insert);
if($insert_query){
return true;
}else{
return false;
}
}
}
public function delete($table,$where = null){
if($this->tableExists($table)){
if($where == null){
$delete = 'DELETE '.$table;
}else{
$delete = 'DELETE FROM '.$table.' WHERE '.$where; //equivalent to DELETE FROM table1 WHERE ......
}
$delete = @mysql_query($delete);
if($delete){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function update($table,$rows,$where){
if($this->tableExists($table)){
for($i = 0; $i < count($where); $i++){
if($i%2 != 0){
if(is_string($where[$i])){
if(($i+1) != null)$where[$i] = '"'.$where[$i].'" AND ';
else $where[$i] = '"'.$where[$i].'"';
}
}
}
$where = implode('',$where);
$update = 'UPDATE '.$table.' SET ';
$keys = array_keys($rows);
for($i = 0; $i < count($rows); $i++){
if(is_string($rows[$keys[$i]])) $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
else $update .= $keys[$i].'='.$rows[$keys[$i]];
// add commas
if($i != count($rows)-1) $update .= ',';
}
$update .= ' WHERE '.$where;
$query = @mysql_query($update);
if($query)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public function getResult(){
return $this->result;
}
private function tableExists($table){
$tableInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
if($tableInDb)
{
if(mysql_num_rows($tableInDb)==1)
{
return true;
}
else
{
return false;
}
}
}
}
?>