class Db
{
var $conn;
function Db($host="localhost",$user="root",$pass="root",$db="test")
{
if(!$this->conn=mysql_connect($host) ,$user,$pass))
die("mysql サーバーに接続できません");
mysql_select_db($db,$this->conn);
mysql_query("SET NAMES 'UTF-8'");
}
関数execute($sql)
{
return mysql_query($sql,$this->conn);
}
function findCount($sql)
{
$result=$this->execute($sql);
return mysql_num_rows($result);
}
関数 findBySql($sql)
{
$array=array();
$result=mysql_query($sql);
$i=0;
while($row=mysql_fetch_assoc($result))
{
$array[$i]=$row;
$i++;
}
$array を返す。
}
//$con的几种情况
//空:返全部记录
//配列:例。 array('id'=>'1') 戻りid=1の记录
//string :eg. 'id=1' 戻りid=1の记录
function toExtJson($table,$start="0",$limit="10",$cons="")
{
$sql=$this->generateSql ($table,$cons);
$totalNum=$this->findCount($sql);
$result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
$resultNum = count($result);//当前結果数
$str="";
$str.= "{";
$str.= "'totalCount':'$totalNum',";
$str.="'行':";
$str.="[";
for($i=0;$i<$resultNum;$i++){
$str.="{";
$count=count($result[$i]);
$j=1;
foreach($result[$i] as $key=>$val)
{
if($j<$count)
{
$str.="'".$key."':'".$値"',";
}
elseif($j==$count)
{
$str.="'".$key."':'".$val."'";
}
$j++;
}
$str.="}";
if ($i != $resultNum-1) {
$str.= ",";
}
}
$str.="]";
$str.="}";
$str を返す;
}
functiongenerateSql($table,$cons)
{
$sql="";//sql条件
$sql="select * from ".$table;
if($cons!="")
{
if(is_array($cons))
{
$k=0;
foreach($cons as $key=>$val)
{
if($k==0)
{
$sql. ="どこ '";
$sql.=$key;
$sql.="'='";
$sql.=$val."'";
}else
{
$sql.="と ' ";
$sql.=$key;
$sql.="'='";
$sql.=$val."'";
}
$k++;
}
}else
{
$sql. =" ここで、.$cons;
}
}
return $sql;
}
関数 toExtXml($table,$start="0",$limit="10",$cons="")
{
$sql=$this->generateSql($table,$cons);
$totalNum=$this->findCount($sql);
$result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
$resultNum = count($result);//当前結果数
header("Content-Type: text/xml");
$xml="n";
$xml.="
n";
$xml.="t".$totalNum."n";
$xml.="tn";
for($i=0;$i<$resultNum;$i++){
$xml.="tt- n";
foreach($result[$i] as $key=>$val)
$xml.="ttt<".$key.">".$val."".$key."> ;n";
$xml.="tt n";
}
$xml.="tn";
$xml.="n";
$xml を返します。
}
//出単語表格
function toWord($table,$mapping,$fileName)
{
header('Content-type: application/doc');
header('Content-Disposition:attachment; filename="'.$fileName.'.doc"');
echo ' xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="[url=http: //www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">
<頭>
'.$fileName.' ';
echo'
';
if(is_array($mapping))
{
foreach($mapping as $key=>$val)
echo''.$val.' | ';
}
echo'
';
$results=$this->findBySql('select * from '.$table);
foreach($results as $result)
{
echo'';
foreach($result as $key=>$val)
echo''.$val.' | ';
echo'
';
}
echo'
';
echo'