1. 程式人生 > >PHP如何查詢MySQL資料頁面並輸出內容到頁面,並另存為CSV或EXCEL檔案

PHP如何查詢MySQL資料頁面並輸出內容到頁面,並另存為CSV或EXCEL檔案

<?​
    header("Content-type:text/html;charset=utf-8");
    /**
     * mysql connection configure
     * 2016-04-09
     * Ron
     */
    $ychat_host = "localhost";
    $ychat_username = "root";
    $ychat_password = "";
    $ychat_database = "talk";
    
    $con = mysql_connect($ychat_host,$ychat_username,$ychat_password) or die ("connection MySQL failed!");
    //select database
    mysql_select_db($ychat_database,$con);
    //sql
    $sql = "select  id,username,password,sex,address,country from userinfo ";
    //set encode
    mysql_query("set names utf8");
    //query results
    $result = mysql_query($sql) or die ("query failed! "+mysql_error);
    //count
    $count = mysql_num_rows($result);
    // number of rows 
    $rows = mysql_affected_rows($con);
    // Number of colums
    $colums = mysql_num_fields($result);
    if($count==0){
        echo "no result!";
    }else{
        echo "total:".$count;
        echo "<table border='1' style='border-collapse:collapse;border:1px solid red;'  cellpadding='1' cellspacing='1'>";
        echo "<tr>";
        for($i = 0;$i < $colums; $i++){
            
            $fieldName = mysql_field_name($result, $i);
            echo "<th>$fieldName</th>";
            
        }
        echo "</tr>";
        while($row=mysql_fetch_row($result)){
            echo "<tr>";
            for($i=0; $i<$colums; $i++){
                echo "<td>$row[$i]</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    
    // release result set 
    mysql_free_result($result);
    //close connection 
    mysql_close($con);
?>

<?
    header("Content-type:text/html;charset=utf-8");
    /**
     * mysel connection configure
     * 2016-04-09
     * Ron
     */
    $ychat_host = "localhost";
    $ychat_username = "root";
    $ychat_password = "";
    $ychat_database = "talk";
    
    $con = mysql_connect($ychat_host,$ychat_username,$ychat_password) or die ("connection MySQL failed!");
    //select database
    mysql_select_db($ychat_database,$con);
    //sql
    $sql = "select  id,username,password,sex,address,country from userinfo";
    //set encode
    mysql_query("set names utf8");
    //query results
    $result = mysql_query($sql) or die ("query failed! "+mysql_error);
    //count
    $count = mysql_num_rows($result);
    
    
    if($count==0){
        echo "no result!";
    }else{
        echo "<table border='1' style='border-collapse:collapse;border:1px solid red;'  cellpadding='1' cellspacing='1'>";
                echo "<tr>";
                echo "<th>id</th><th>使用者名稱</th><th>性別</th><th>地址</th><th>國家</th>";
                echo "</tr>";
                while ($userinfo = mysql_fetch_array($result)) {
                    $id= $userinfo["id"];
                    $name= $userinfo["username"];
                    $sex= $userinfo["sex"];
                    $address= $userinfo["address"];
                    $country= $userinfo["country"];
                    echo "<tr>";
                    echo "<td>$id</td><td>$name</td><td>$sex</td><td>$address</td><td>$country</td>";
                    echo "</tr>";
                }
                echo "</table>";
        
    }
    
    // release result set 
    mysql_free_result($result);
    //close connection 
    mysql_close($con);
?>