所以我有一个excel文件。我有多个返回的查询。但是,我需要立即运行所有这些查询并将结果输出到excel文件。
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$h = preg_split('/X/', $value);
if ($h[0] = $id and $h[2] != null){
$results="select question from lime_questions where sid =".$h[0]." and gid =".$h[1]." and qid =".$h[2]."; ";
echo $results;
//This is where the queries are returned.
//They are echoed to the first cell of the excel file
//They are returned as "query1;query2;..."
//This is where I am messing up.
//I am attempting to run the queries. I have been attempting many different approaches
$query_result = mysql_query($results[0]); //this does not return results of the queries
echo $query_result;
//attempting to show the results in the first cell of the excel file
}
//ideally at this point this foreach would print each query result in its own cell
foreach(mysql_query($results) as $value2){
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value2);
$row++;
}
$row = 1;
$col++;
}
答案 0 :(得分:2)
您可以在PHP 5中尝试mysqli_multi_query。
http://us2.php.net/manual/en/mysqli.multi-query.php
除此之外,嵌套查询“应该”起作用。您可能有错误的键/值元素被定位。如果不能访问正在使用的模式,我很难说。
但我同意,旧功能将弃用,因此需要使用更新的脚本方法进行更新。
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>