简体   繁体   English

可以将多表MySQL结果下载到.csv

[英]Can download multi table MySQL result to .csv

I'm working with PHP and MySQL. 我正在使用PHP和MySQL。 I am trying to run a MySQL query from 2 tables within my database, then download the results to a CSV. 我正在尝试从数据库中的2个表运行MySQL查询,然后将结果下载到CSV。 When I run the MySQL query through phpMyAdmin, the results are what I am looking for, but when I run through php page, I get an error: 当我通过phpMyAdmin运行MySQL查询时,结果就是我想要的结果,但是当我通过php页面运行时,出现错误:

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

Obviously, the are no results given, and I get a blank .csv file when running the .php program/page. 显然,没有给出结果,运行.php程序/页面时出现空白的.csv文件。 I've literally been tinkering with this every day since last Thursday, and would greatly appreciate any guidance or help. 从上周四开始,我每天都在努力地进行修改,非常感谢任何指导或帮助。 Here is what I have so far. 这是我到目前为止所拥有的。

    <?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";}
    ?>

The ) at the end shouldn't be there on amz_parts. 最后的)不应放在amz_parts上。 mfr-code )"; 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`";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM