PHP:在函数内创建 MYSQL 查询

标签 php mysql

我有一个执行 SQL 查询的函数,“queryValue”稍后传递要执行的实际查询。

Function queryCreate($queryValue){
    //DB connection variables
    $host = "";
    $user = "";
    $password = "";
    $database = "";

    //create the connection
    $conn = new mysqli($host, $user, $password, $database);

    if ($conn->connect_error) {
        die('DB Connection error: (' . $conn->connect_error . ') ' . $conn->connection_errorno);
    }
    $query = mysqli_query($conn,$queryValue) or die(mysqli_error($conn));

    if ($result = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
        fputcsv($fh, array_keys($result));
        fputcsv($fh, $result);
        while ($result = mysqli_fetch_array($query,MYSQLI_ASSOC)) {     
            fputcsv($fh, $result);
        }
    }
    return $queryValue;
}

然后我尝试在单独的 if 语句中分配查询值。下面:

if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight')) {
    $fh = csvCreate("Output Weight Null ".date('m-d-Y-His').".csv");
    $queryValue = queryCreate('SELECT * FROM  `table` WHERE WEIGHT = 0 OR weight IS NULL');
}

我遇到的问题是查询似乎没有传递给函数。谁能建议我在这里哪里出错了?非常感谢。

csvCreate 函数如下所示:

   function csvCreate($filename){
   header("Cache=Control: must-revalidate, post-check=0, pre-check=0");
   header('Content-Description: File Transfer');
   header("Content-type: text/csv");
   header("Content-Disposition: attachment; filename={$filename}");
   header("Expires: 0");
   header("Pragma: public");
   $fh = @fopen( 'php://output', 'w' );
   return $fh;
}

最佳答案

问题在于 queryCreate() 函数中 fputcsv() 调用的参数。

文件处理程序($fh 变量)使用 csvCreate() 函数在 queryCreate() 函数之外声明:

$fh = csvCreate("Output Weight Null ".date('m-d-Y-His').".csv");

但是,$fh 没有作为参数传递给 queryCreate(),也没有将 $fh 声明为全局变量,但是 $fh 变量用于在所有 fputcsv() 调用中引用文件:

fputcsv($fh, array_keys($result));

在这种情况下,queryCreate() 中的 $fh 将不会引用调用 queryCreate() 的 $fh 变量,但它会创建一个本地 $fh 变量(此时为空),因此 fputcsv() 调用将失败. csv 文件在 csvCreate() 中创建,这独立于将值放入文件中。

最好的解决方案是将 $fh 作为参数传递给 queryCreate(),或者从 queryCreate() 调用 csvCreate()。在后一种情况下,数据集的名称应作为参数传递。

更新 让我们也看看一些代码:

//declaration of queryCreate()
Function queryCreate($queryValue, $reportName){ //$reportName is the name of the report
    ...
    //create the csv file, put some parameter checks here as well
    $fh = csvCreate($reportName.date('m-d-Y-His').".csv");
    //and some error handling here
    ...
    //output the contents of the query to the csv file
    if ($result = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
        fputcsv($fh, array_keys($result));
        fputcsv($fh, $result);
        while ($result = mysqli_fetch_array($query,MYSQLI_ASSOC)) {     
            fputcsv($fh, $result);
        }
    }       
    ... //should include closing of the csv file
} //end queryCreate()

...

//call the queryCreate()
if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight')) {
    $queryValue = queryCreate('SELECT * FROM  `table` WHERE WEIGHT = 0 OR weight IS NULL','Output Weight Null ');
}

关于PHP:在函数内创建 MYSQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480081/

相关文章:

php - csv 不会忽略 ','(逗号)并使用 php 创建新行

mysql - 多对多表 SQL

mysql - 无法通过socket '/tmp/mysql.sock'连接到本地MySQL服务器(2)

php - 发表评论脚本

php - 此帐户未设置 PayPal Express Checkout

php - mysql_insert_id 似乎从来没有工作过

php - 价格中缺少 2 位小数(PHP)

php - 保存、组织和查询产品、选项/标签和类别

php - 标记约定

php - 查询输出中的问题 - php+json