dso15221 2011-07-25 15:55
浏览 59

使用PHP将数据从SQl导出到Excel

So the admin has the choice to choose what he want to export to excel by selecting checkboxes which i stored in col[]... here's my code for exporting

 session_start();
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'fyp_db';

$link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB);

if (is_array($_POST['col'])) {
    $sql = "SELECT ";
    foreach ($_POST['col'] AS $value) {
        $sql .= "{$value}, ";
    }
    $sql = substr($sql, 0, -2);
    $sql .= " FROM account, coursedetail, coursecategory";  
    /*echo "sql= " . $sql . "<br /><br />
";*/
} else {
    echo "No column was selected<br /><br />
";
}

function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/?
/", "\
", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; }

$filename = "website_data.xls"; 
header("Content-Type: text/plain"); 
$flag = false; 
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
while(false !== ($row = mysql_fetch_assoc($result))) { 
    if(!$flag) { 
    // display field/column names as first row 
    echo implode("\t", array_keys($row)) . "
"; 
    $flag = true; 
        } 
        array_walk($row, 'cleanData'); 
        echo implode("\t", array_values($row)) . "
"; 

        }

I got the error of..

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project\export_successful.php on line 28

why? :(

  • 写回答

1条回答 默认 最新

  • dpxpz37157 2011-07-25 15:58
    关注

    You're mixing up mysqli and mysql calls. The two libraries are NOT compatible and handles/statements returned by one cannot be used in the other.

    $result = mysqli_query($link, $sql) or die(mysqli_error($link));
                   ^--- note the 'i'
    while(false !== ($row = mysql_fetch_assoc($result))) { 
                                 ^--- note the LACK of an 'i'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图