php-crud-oop/database.php

232 lines
7.4 KiB
PHP
Raw Permalink Normal View History

2020-01-02 23:24:47 +07:00
<?php
class Database
{ private $db_host = 'localhost';
private $db_user = 'project';
private $db_pass = '';
private $db_name = 'rai_crud';
private $connection = false;
protected $result = array();
public function connect(){
if(!$this->connection){
$mycon = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if($mycon){
$selectdb = @mysql_select_db($this->db_name,$mycon);
if($selectdb){
$this->connection = 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, $order = null){
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if($order != null) $q .= ' ORDER BY '.$order;
//echo $q;
if($this->tableExists($table)){
//echo $q;
$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 selectwithpaging($table, $limit = null, $hal = null,$where = null){
$rows = '*';
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null) $q .= ' WHERE '.$where;
if (!empty($hal)) {
$page = $hal - 1;
$MulaiAwal = $limit * $page;
} else if (!empty($hal) and $hal == 1) {
$MulaiAwal = 0;
} else if (empty($hal)) {
$MulaiAwal = 0;
}
$q .= ' LIMIT '.$MulaiAwal.' , '.$limit;
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.')';
echo $insert;
$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;
//echo $update;
$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;
}
}
}
}
?>