【问题标题】:Can download multi table MySQL result to .csv可以将多表 MySQL 结果下载到 .csv
【发布时间】:2013-09-03 12:35:13
【问题描述】:

我正在使用 PHP 和 MySQL。我正在尝试从我的数据库中的 2 个表运行 MySQL 查询,然后将结果下载到 CSV。当我通过 phpMyAdmin 运行 MySQL 查询时,结果是我正在寻找的结果,但是当我通过 php 页面运行时,我得到一个错误:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /..../..../...../workingpage.php on line 26

显然,没有给出任何结果,并且在运行 .php 程序/页面时得到一个空白的 .csv 文件。自上周四以来,我每天都在修补这个问题,非常感谢任何指导或帮助。这是我目前所拥有的。

    <?php
    include("includes/credentials.inc");

    $con = mysqli_connect($host, $user, $password, $database)
           or die ("COULDN'T CONNECT TO MY SQL SERVER");


    $query = "SELECT (

    SELECT CONCAT( manufacturer,  '-', partnumber ) AS sku
    )sku, weight, sell
    FROM  `amz_parts` 
    INNER JOIN  `amz_mfg` ON amz_mfg.`mfr-code` = amz_parts.`mfr-code`)";



    $result = mysqli_query($con, $query); 

    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=file.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    function maybeEncodeCSVField($result) {
        if(strpos($result, ',') !== false || strpos($result, '"') !== false ||                                 strpos($result, "\n") !== false) {
    $result = '"' . str_replace('"', '""', $result) . '"';
        }
        return $result;
    }

    while ($row = mysqli_fetch_array($result)) {
        echo "$row[0] $row[1] $row[2]\n";}
    ?>

【问题讨论】:

  • mysqli_error($con) 是否显示错误?
  • $result 是否可能失败,这就是为什么它是boolean false? 1. 尝试检查您的credentials.inc 是否正确包含在$con 中 2. 尝试通过var_dump($result); 转储您的$result
  • 尝试在第 15 行添加 mysqli_error($con),在第 19 行添加 mysqli_error($result)。屏幕上没有显示错误,但是在服务器端 error_log 中,显示 mysqli_error() 需要参数 1是 mysqli,布尔值在第 19 行的.....php 中给出,mysqli_fetch_array() 期望参数 1 是 mysqli_result,布尔值在第 33 行的 /.....php 中给出

标签: php mysql csv export-to-csv


【解决方案1】:

最后的 ) 不应该出现在 amz_parts.mfr-code)";

$query = "SELECT (

    SELECT CONCAT( manufacturer,  '-', partnumber ) AS sku
    )sku, weight, sell
    FROM  `amz_parts` 
    INNER JOIN  `amz_mfg` ON amz_mfg.`mfr-code` = amz_parts.`mfr-code`";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多