Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:在函数内创建MYSQL查询_Php_Mysql - Fatal编程技术网

PHP:在函数内创建MYSQL查询

PHP:在函数内创建MYSQL查询,php,mysql,Php,Mysql,我有一个执行SQL查询的函数,“queryValue”稍后传递要执行的实际查询 Function queryCreate($queryValue){ //DB connection variables $host = ""; $user = ""; $password = ""; $database = ""; //create the connection $conn = new mysqli($host, $user, $passwo

我有一个执行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()调用的参数

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

$fh = csvCreate("Output Weight Null ".date('m-d-Y-His').".csv");
但是,$fh不会作为参数传递给queryCreate(),也不会将$fh声明为全局变量,但$fh变量在所有fputcsv()调用中都用于引用文件:

在这种情况下,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 ');
}

您如何知道参数没有传递给函数?为什么返回查询作为函数的结果?这可能是一种假设。CSV显示为空白。我试图将“queryValue”传入IF语句中调用的函数。您是否尝试直接从您最喜欢的mysql管理器应用程序(例如phpmyadmin)运行查询?如果是,你有没有拿回任何记录?$fh变量(用于将数据输出到csv的文件处理程序)从何而来?如果未在函数中声明为全局变量,php将假定$fh为局部变量。在phpMyAdmin中使用时,查询将返回值。现在包含了文件流的函数。这是因为正在创建一个具有正确标题的文件,只是没有填充它。感谢您在运行此程序时提供了完整的错误报告和显示,因此您可以看到所有可能的警告和通知吗?请您给我一个示例,说明如何使用后一种解决方案。我有点迷路了。Cheers这一部分给您带来了一个问题:向函数添加另一个参数或将函数调用从一个位置移动到另一个位置?在queryCreate()中调用csvCreate,我不确定如何动态执行该操作,因为我需要在if语句中指定fh文件名。
//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 ');
}