php对数据库的基本操作
<?php
class sqldb
{
public $DB;
DBNAME
function conetdb()
{
$this->DB=new mysqli('localhost','$ACC','$PWD','$DBNAME','3306')or die("Erro from DB 0");
}
function read_alldata_tab($tabname,$where='')
{
$tabname=SQLHEAD.$tabname;
//echo $tabname;
if($where=='')
{
$sql="SELECT *FROM ".$tabname;
}else
{
$a=count($where);
$keys=array_keys($where);
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' AND ";
}else
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' ";
}
}
$sql="SELECT *FROM ".$tabname." WHERE ".$w;
}
$result=$this->DB->query($sql);
if ($result->num_rows > 0)
{
$c=0;
while($row = $result->fetch_assoc())
{
$d[$c]=$row;
$c++;
}
return $d;
}
}
function add_data_tab($tabname,$arr)
{
$tabname=SQLHEAD.$tabname;
$a=count($arr);
$keys=array_keys($arr);
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$column.=$keys[$b].",";
}else
{
$column.=$keys[$b]." ";
}
}
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$value.="'".$arr[$keys[$b]]."',";
}else
{
$value.="'".$arr[$keys[$b]]."' ";
}
}
$sql="INSERT INTO ".$tabname."(".$column.") VALUES (".$value.")";
//echo $sql;
return $result=$this->DB->query($sql);
}
function updata_tab($tabname,$where,$arr)
{
$tabname=SQLHEAD.$tabname;
$a=count($arr);
$keys=array_keys($arr);
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$column.=$keys[$b]."="."'".$arr[$keys[$b]]."',";
}else
{
$column.=$keys[$b]."="."'".$arr[$keys[$b]]."' ";
}
}
$a=count($where);
$keys=array_keys($where);
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' AND ";
}else
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' ";
}
}
$sql="UPDATE ".$tabname." set ".$column." WHERE ".$w;
//echo $sql;
return $result=$this->DB->query($sql);
}
function del_tab($tabname,$where)
{
$tabname=SQLHEAD.$tabname;
$a=count($where);
$keys=array_keys($where);
for($b=0;$b<$a;$b++)
{
if($b!=$a-1)
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' AND ";
}else
{
$w.=$keys[$b]."="."'".$where[$keys[$b]]."' ";
}
}
$sql="DELETE FROM ".$tabname." WHERE ".$w;
return $result=$this->DB->query($sql);
}
}
$sqldb=new sqldb;//全站数据库对象
$sqldb->conetdb();//连接数据库
?>
符攀飞
接近 3 年前
361