Untuk membuat data dengan ubuntu
<?php
error_reporting(1); // error ditampilkan
class Database
{ private $host="";
private $dbname="";
private $user="root";
private $password="";
private $port="3306";
private $conn;
// function yang pertama kali di-load saat class dipanggil
public function __construct()
{ // koneksi database
try
{ $this->conn = new PDO("mysql:host=$this->host;port=$this->port;dbname=$this->dbname;charset=utf8",$this->user,$this->password);
} catch (PDOException $e)
{ echo "Koneksi gagal";
}
}
public function tampil_data($id_barang){
$query = $this->conn->prepare("SELECT * FROM WHERE =?");
$query->execute(array($id_barang));
// mengambil satu data dengan fetch
$data = $query->fetch(PDO::FETCH_ASSOC);
// mengembalikan data
return $data;
// hapus variable dari memory
$query->closeCursor();
unset($id_barang,$data);
}
public function tampil_semua_data(){
$query = $this->conn->prepare("SELECT * FROM ");
$query->execute();
// mengambil banyak data dengan fetchAll
$data = $query->fetchAll(PDO::FETCH_ASSOC);
return $data;
$query->closeCursor();
unset($data);
}
public function tambah_data($data){
$query = $this->conn->prepare("insert ignore into barang () values (?, ?, ?, ?)");
$query->execute(array($data[''], $data[''], $data[''], $data['']));
$query->closeCursor();
unset($data);
}
public function ubah_data($data)
{
$query = $this->conn->prepare("update set ? where =?");
$query->execute(array($data[''],$data['']));
$query->closeCursor();
unset($data);
}
public function hapus_data($data)
{ $query = $this->conn->prepare("delete from where =?");
$query->execute(array($data));
$query->closeCursor();
unset($data);
}
}
?>
Komentar
Posting Komentar