将数据库的内容读到二维数组并按指定列输出

在oso的工程师的提示下写了一个导航程序。

单表备份 代码:
复制代码 代码如下:

$conn=mysql_connect(“$host”,”$user”,”$password”) or
die(“数据库打开出错”); mysql_select_db(“$database”);

class Db   
{   
    var $conn;   

$query=”select * from $tables”; $result=mysql_query($query,$conn);
$i=0; $j=0; while($row=mysql_fetch_row($result)){ $array[$i][$j] =
$row[0].$row[1]; $array2[$i][$j] = $row[0]; $j++; // echo
$i.”,”.$j.$row[1].””; if($j==3) { $i++; $j=0; } //else {$j++ ;}

    function
Db($host=”localhost”,$user=”root”,$pass=”root”,$db=”test”)   
    {   
      if(!$this->conn=mysql_connect($host,$user,$pass))   
      die(“can’t connect to mysql sever”);   
      mysql_select_db($db,$this->conn);   
      mysql_query(“SET NAMES ‘UTF-8′”);   
    }   

} $amax=count($array);//获是数组内数据个数。

    function execute($sql)   
    {   
       return mysql_query($sql,$this->conn);   
    }   

$rows=2; //设置列数

    function findCount($sql)   
    {   
        $result=$this->execute($sql);   
        return mysql_num_rows($result);   
    }   

//开始显示数据 for ($x=0; $x<=$amax-1; $x++) { for ($y=0; $y<=
$rows; $y++) { echo “”.$array[$x][$y].”|” ; } echo “

    function findBySql($sql)   
    {   
        $array=array();   
        $result=mysql_query($sql);   
        $i=0;   
        while($row=mysql_fetch_assoc($result))   
           {   
          $array[$i]=$row;    
       $i++;   
           }   
        return $array;   
    }   

“; }

    //$con的几种情况   
    //空:返回全部记录   
    //array: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.=”‘rows’:”;   
      $str.=”[“;   
      for($i=0;$i       $str.=”{“;    
       $count=count($result[$i]);   
       $j=1;   
       foreach($result[$i] as $key=>$val)   
       {   
       if($j       {   
       $str.=”‘”.$key.”‘:'”.$val.”‘,”;   
       }   
       elseif($j==$count)   
       {   
       $str.=”‘”.$key.”‘:'”.$val.”‘”;   
       }   
       $j++;   
                }   

/* 显示结果为 aaa|bbb|ccc ddd|eee|fff lll|mmm|nnn ….. */

       $str.=”}”;   
       if ($i != $resultNum-1) {   
                 $str.= “,”;   
             }   
      }   
      $str.=”]”;   
      $str.=”}”;   
      return $str;     
    }   

?>

    function 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.=” where “.$cons;   
       }   
       }   
       return $sql;   
    }   

    function 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”;   
澳门新葡亰3522平台游戏,       $xml.=”t”.$totalNum.”n”;   
       $xml.=”tn”;   
       for($i=0;$i       $xml.=”ttn”;   
       foreach($result[$i] as $key=>$val)   
       $xml.=”ttt”.$val.””.$key.”>n”;   
       $xml.=”ttn”;   
       }   
        $xml.=”tn”;   
        $xml.=”n”;   
        return $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=”[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”;   
        echo”;   
    }   

    function toExcel($table,$mapping,$fileName)   
    {   
      header(“Content-type:application/vnd.ms-excel”);   
         header(“Content-Disposition:filename=”.$fileName.”.xls”);   
      echo’          
xmlns:x=”urn:schemas-microsoft-com:office:excel” 
          
xmlns=”[url=;   
              
          

   
          

   
              
              
        ‘;    
        echo’

‘;   
           echo’