PHP ~ MySQL DB Class

define('HOSTNAME', 'localhost');
define('USERNAME', 'root'); 
define('PASSWORD', '12321313213123');
define('DATABASE', 'gold');  

 class DB
 {
  private $dbLink;
  private $result;
  
  public function __construct()
  {
   $this->connect();
  }
  
  protected function connect()
  {
   $this->dbLink = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATABASE);
  }
  
  public function query($sql)
  {
   $this->result = mysqli_query($this->dbLink, $sql);
  }
  
  public function fetch()
  {
   return mysqli_fetch_array($this->result);
  }
  
  public function close()
  {
   mysqli_close($this->dbLink);
  }
  
 }
 
$db = new DB();
$sql = "SELECT * FROM prices";
$db->query($sql);
while($x = $db->fetch())
{
 print $x['buy'].',';
}
$db->close();

沒有留言: