PHP-MySQL : Membuat Class Database
Kali ini kita akan membuat Class Database menggunakan PHP, dimana class ini berfungsi untuk mengatur segala aktifitas yang berhubungan dengan database.
001 | class mySql { |
002 | private $DBhost ; //your internet address of the mySQL database your going to use. |
003 | private $DBusername ; //your mysql User name of the mySQL database your going to use. |
004 | private $DBpassword ; //your mysql password of the mySQL database your going to use. |
005 | private $DBname ; //name of database |
006 | private $link_id ; |
007 | private $sql ; |
008 | private $result ; |
009 | private $num_rows ; |
010 | private $row ; |
011 | private $l_field ; |
012 | private $field_define ; |
013 | private $NOL ; |
014 |
015 | function mySql(){ //__construct-or |
016 | $this ->DBhost = "localhost" ; |
017 | $this ->DBusername = "root" ; |
018 | $this ->DBpassword = "" ; |
019 | $this ->DBname = "dbname" ; |
020 | $this ->NOL= "0" ; |
021 | } |
022 | // mendapatkan pengguna database |
023 | public function getDBuid(){ |
024 | return $this ->DBusername; |
025 | } |
026 | // mendapatkan host database |
027 | public function getDBhost(){ |
028 | return $this ->DBhost; |
029 | } |
030 | // mendapatkan password database |
031 | public function getDBpass(){ |
032 | return $this ->DBpassword; |
033 | } |
034 | // mendapatkan nama database |
035 | public function getDBname(){ |
036 | return $this ->DBname; |
037 | } |
038 | // memulai koneksi |
039 | public function openKoneksi(){ |
040 | $this ->link_id=mysql_connect( $this ->DBhost, $this ->DBusername, $this ->DBpassword); |
041 | return mysql_select_db( $this ->DBname, $this ->link_id) or die ( "UNABLE TO SELECT DATABASE" ); |
042 | } |
043 |
044 | // DDL |
045 | // melakukan eksekusi sql |
046 | function execute( $query ){ |
047 | $this ->sql= $query ; |
048 | $this ->result=mysql_query( $this ->sql) or die (mysql_error()." |
049 | ". $query ); |
050 | } |
051 | // menghapus record |
052 | function deleteRows( $isTable , $isCriteria ){ |
053 | return $this ->execute( "DELETE FROM $isTable WHERE $isCriteria" ) or die (mysql_error()); |
054 | } |
055 | // mengupdate record |
056 | function updateRows( $isTable , $isValues , $isCriteria ){ |
057 | return $this ->execute( "UPDATE $isTable SET $isValues WHERE $isCriteria" ); |
058 | } |
059 | // memasukan data baru |
060 | function insertRows( $isTable , $isValues ){ |
061 | $tmp = "INSERT INTO $isTable VALUES(" . $isValues . ")" ; |
062 | return $this ->execute( $tmp ); |
063 | } |
064 | // memasukan hasil query ke dalam array |
065 | function getArray(){ |
066 | $this ->row=mysql_fetch_array( $this ->result,MYSQL_NUM); |
067 | return $this ->row; |
068 | } |
069 |
070 | // mencari nilai 1 kolom |
071 | function getField( $thisField , $thisTable , $thisCriteria , $thisValue ){ |
072 | $Q = $this ->execute( "SELECT $thisField FROM $thisTable WHERE $thisCriteria='$thisValue'" ); |
073 | $R = $this ->getArray(); |
074 | return $R [0]; |
075 | } |
076 | // menghitung record keseluruhan |
077 | function record_count(){ |
078 | $this ->num_rows=mysql_num_rows( $this ->result); |
079 | return $this ->num_rows; |
080 | } |
081 |
082 | // Operasi DCL |
083 | // mendapatkan list field dari suatu kolom |
084 | function list_field( $table_name ){ |
085 | $this ->l_field=mysql_list_fields( $this ->DBname, $table_name ); |
086 | $this ->l_field; |
087 | } |
088 | // menghitung jumlah kolom |
089 | function num_fields(){ |
090 | $this ->field_count=mysql_num_fields( $this ->l_field); |
091 | return $this ->field_count; |
092 | } |
093 |
094 | //membuat nomor otomatis berformat |
095 | function auto_number( $field , $table , $key , $Parse , $Digit_Count ){ |
096 | $this ->NOL= "0" ; |
097 | $this ->execute( "Select $field from $table where $key like '$Parse%' order by $key DESC" ); |
098 | $counter =2; |
099 | if ( $this ->record_count()==0) |
100 | { |
101 | while ( $counter < $Digit_Count ) { $this ->NOL= "0" . $this ->NOL; |
102 | $counter ++; |
103 | } |
104 | return $Parse . $this ->NOL. "1" ; |
105 | } |
106 | else |
107 | { |
108 | $R = $this ->getArray(); |
109 | $K = sprintf( "%d" , substr ( $R [0],- $Digit_Count )); |
110 | $K = $K + 1; |
111 | $L = $K ; |
112 | while ( strlen ( $L )!= $Digit_Count ) |
113 | { |
114 | $L = $this ->NOL. $L ; |
115 | } |
116 | return $Parse . $L ; |
117 | } |
118 | } |
119 | } |
Comments
Powered by Facebook Comments