-
- class Db
- {
- var $conn;
-
- 함수 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'");
- }
-
- 함수 실행($sql)
- {
- return mysql_query($sql,$this->conn);
- }
-
- function findCount($sql)
- {
- $result=$this->execute($sql);
- return mysql_num_rows($result);
- }
-
- function findBySql($sql)
- {
- $array=array();
- $result=mysql_query($sql);
- $i=0;
- while($row=mysql_fetch_assoc($result))
- {
- $array[$i]=$row;
- $i ;
- }
- return $array;
- }
-
- //$con적几种情况
- //공:返回전체부记录
- //배열:eg. 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."':'".$val."',";
- }
- elseif($j==$count)
- {
- $str.="'".$key."':'".$val."'";
- }
- $j ;
- }
-
- $str.="}";
- if ($i != $resultNum-1) {
- $str.= ",";
- }
- }
- $str.="]";
- $str.="}";
- $str을 반환합니다;
- }
-
- 함수 generateSql($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.="where '";
- $sql.=$key;
- $sql.="'='";
- $sql.=$val."'";
- }else
- {
- $sql.="and '";
- $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."".$ 키.">n";
- $xml.="ttn";
- }
- $xml.="tn";
- $xml.="n";
- $xml을 반환합니다.
- }
-
- //输出word表格
- 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= "http://www.w3.org/TR/REC-html40">
- <머리>
-
- '.$fileName.'
-
- ';
- echo'<테이블 테두리=1>
';
- 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'
|