|
|
|
ช่วยอธิบายให้หน่อยได้ไหมครับ อ่นแล้วบางอันเข้าใจบางอันก็งงๆ |
|
|
|
|
|
|
|
Code (PHP)
class DB
{
var $host = DB_HOST;
var $user = DB_USERNAME;
var $pass = DB_PASSWORD;
var $base = DB_NAME;
var $lang = DB_LANG;
var $dane;
var $group;
var $join;
var $order;
var $pola;
var $sql;
var $tabela;
var $where;
// àª×èÍÁµèÍ°Ò¹¢éÍÁÙÅ
function DB($host = DB_HOST, $user = DB_USERNAME, $pass = DB_PASSWORD, $base = DB_NAME, $lang = DB_LANG){
// $this->connect($this->host,$this->user,$this->pass,$this->base,$this->lang);
$this->connect($host, $user, $pass, $base, $lang);
}
// àª×èÍÁµèÍ°Ò¹¢éÍÁÙÅ
function connect($host,$user,$pass,$base,$lang){
//ob_start(); start
$this->sql = mysql_connect($host,$user,$pass) or die("Access denied for user: '".$user."'@'".$host."' ");
@mysql_query($lang);
mysql_select_db($base) or die("Base error");
return true;
}
function disconnect(){
mysql_close($this->sql);
}
// $db->add("tabel",array("field"=>"value"));
// np: $db->add('test',array("a"=>"b","c"=>"d"));
function add($tabela,$dane){
$key = array_keys($dane);
$value = array_values($dane);
$il = count($key);
for ($i=0;$i<$il;$i++)
{
if (empty($ad)){
$ad="(";
}else{
$ad=$ad.",";
}
if (empty($val)){
$val="(";
}else{
$val=$val.",";
}
$ad=$ad.$key[$i];
$val=$val."'".$value[$i]."'";
}
$ad=$ad.")";
$val=$val.")";
$sql="INSERT INTO ".$tabela." ".$ad." VALUES ".$val;
if (mysql_query($sql)){
return true;
}else{
$this->_error();
return false;
}
}
// $db->update("tabel",array("field"=>"value"),"where");
// np: $db->update('test',array("a"=>"g","c"=>"h"),"a='b'");
function update($tabela,$dane,$where=""){
$key = array_keys($dane);
$value = array_values($dane);
$il = count($key);
$set="";
for ($i=0;$i<$il;$i++)
{
if (!empty($set)){
$set=$set.",";
}
$set=$set.$key[$i]."='".$value[$i]."'";
}
$sql="UPDATE ".$tabela." SET ".$set." WHERE ".$where;
if (mysql_query($sql)){
return true;
}else{
$this->_error();
return false;
}
}
// $db->IsUpdate("tabel","set","where");
// np: $db->IsUpdate('test',"pageview=pageview+1","a='g'");
function IsUpdate($tabela,$set,$where=""){
$sql="UPDATE ".$tabela." SET ".$set." WHERE ".$where;
if (mysql_query($sql)){
return true;
}else{
$this->_error();
return false;
}
}
// $db->del("tabel","where");
// np: $db->del('test',"a='g'");
function del($tabela,$where=""){
$sql="DELETE FROM ".$tabela." WHERE ".$where;
if (mysql_query($sql)){
return true;
}else{
$this->_error();
return false;
}
}
// $db->num_row("tabel","field","where");
// np: $db->num_row('test','a',null);
function num_row($tabela,$pole,$where="") {
if ($where=="") {
$where = "";
} else {
$where = " WHERE ".$where;
}
$sql = "SELECT COUNT(".$pole.") FROM ".$tabela.$where;
if($res = mysql_query($sql)){
return mysql_num_rows($res);
}else{
$this->_error();
return false;
}
}
// $db->num_rows("tabel","field","where");
// np: $db->num_rows('test','a',null);
function num_rows($tabela,$pole,$where="") {
if ($where=="") {
$where = "";
} else {
$where = " WHERE ".$where;
}
$sql = "SELECT ".$pole." FROM ".$tabela.$where;
if($res = mysql_query($sql)){
return mysql_num_rows($res);
}else{
$this->_error();
return false;
}
}
// $res = $db->select('test',array('a','c'),null,null,null);
// while ($arr = $db->fetch($res)) {
// echo $arr['a']." - ".$arr['c']."<br>\n";
// }
function fetch($sql){
//return mysql_fetch_assoc($sql);
if($sql == false) return false;
if ($res = mysql_fetch_assoc($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
// $a = $db->fetch_row($res);
// $res_id = $a[0];
function fetch_row($sql){
//return mysql_fetch_row($sql);
if ($res = mysql_fetch_row($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
// $NRow = $db->nrow($res);
function nrow($sql){
//return mysql_num_rows($sql);
if ($res = mysql_num_rows($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
// $res = $db->select('test',array('a','c'),null,null,null);
// $rows = $db->rows($res);
function rows($sql){
//return mysql_num_rows($sql);
if ($res = mysql_num_rows($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
// $res = $db->select_query('SELECT pola FROM tabela WHERE warunek');
function select_query($sql){
if ($res = mysql_query($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
// $db->select('tabela',array("pole","pole"),"where","join","order by","group by");
// np: $db->select('test',array("a","c"),null,null,null,null);
function select($tabela,$pola,$where="",$join="",$order="",$group=""){
foreach ($pola as $pole)
{
if (!empty($ob)){
$ob=$ob.",";
}else{
$ob="";
}
$ob=$ob.$pole;
}
if (isset($join)){
$jo=" ".$join." ";
}else{
$jo=" ";
}
if (isset($where)){
$war=" WHERE ".$where;
}else{
$war="";
}
if (isset($where)){
$war=" WHERE ".$where;
}else{
$war="";
}
if (isset($order)){
$ord=" ORDER BY ".$order;
}else{
$ord="";
}
if (isset($group)){
$gr=" GROUP BY ".$group;
}else{
$gr="";
}
$sql="SELECT ".$ob." from ".$tabela.$war.$jo.$ord.$gr;
if ($res = mysql_query($sql)){
return $res;
}else{
$this->_error();
return false;
}
}
function _error(){
$this->error[]=mysql_errno();
}
function insert_id(){
return mysql_insert_id($this->sql);
}
}
Tag : PHP
|
|
|
|
|
|
Date :
2011-01-31 03:40:30 |
By :
เด็กทำโปรเจค |
View :
881 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|