Skip to content

Instantly share code, notes, and snippets.

@ruliarmando
Created May 6, 2014 14:52
Show Gist options
  • Save ruliarmando/76694b26bfe540f452c9 to your computer and use it in GitHub Desktop.
Save ruliarmando/76694b26bfe540f452c9 to your computer and use it in GitHub Desktop.
kelas Database MySQL
<?php
class Db
{
private $conn;
private $query;
private $result;
public function __construct($host, $username, $password, $db_name)
{
$this->conn = mysql_connect($host, $username, $password);
if(!$this->conn){
die("Tidak bisa terkoneksi ke MySQL");
}
$selected = mysql_select_db($db_name, $this->conn);
if(!$selected){
die("Tidak bisa terkoneksi ke database: $db_name");
}
}
public function row($query)
{
$this->query = $query;
$this->$result = mysql_query($this->query, $this->conn);
if(!$this->result){
die("Query error : ".mysql_error());
}
return mysql_fetch_row($this->result);
}
public function all($query)
{
$this->query = $query;
$this->result = mysql_query($this->query, $this->conn);
if(!$this->result){
die("Query error : ".mysql_error());
}
$all_rows = array();
while($row = mysql_fetch_assoc($this->result)){
$all_rows[] = $row;
}
return $all_rows;
}
public function num_rows($query = null)
{
if($query !== null){
$result = mysql_query($query, $this->conn);
return mysql_num_rows($result);
}
return mysql_num_rows($this->result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment