<?php
class SqlHelper{
private $host;
private $user;
private $pwd;
private $dbName;
private $charset;
private $conn = null;
public function __construct(){
$this->host = 'localhost';
$this->user = 'xuwm';
$this->pwd = 'bW7LA2pMDAEtnVB7';
$this->dbName = 'gamejj';
$this->charset='utf8';
$this->connect($this->host, $this->user, $this->pwd);
$this->switchDb($this->dbName);
$this->setChar($this->charset);
}
private function connect($host, $user, $pwd){
$conn = mysql_connect($host, $user, $pwd);
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
$this->conn = $conn;
}
public function switchDb($db){
$sql = 'use ' . $db;
$this->query($sql);
}
public function setChar($char){
$sql = 'set names ' . $char;
$this->query($sql);
}
public function close(){
mysql_close($this->conn);
}
public function query($sql){
$result = mysql_query($sql, $this->conn);
return $result;
}
public function getAll($sql){
$list = array();
$result = $this->query($sql);
if(!$result)
return false;
while($row= mysql_fetch_assoc($result)){
$list[] = $row;
}
return $list;
}
public function getRow($sql){
$result = $this->query($sql);
if(!$result)
return false;
$row= mysql_fetch_assoc($result);
return $row;
}
public function getOne($sql){
$result = $this->query($sql);
if(!$result)
return false;
$row= mysql_fetch_row($result);
return $row[0];
}
}