2012-11-08 85 views
-3

所以我有一个excel文件。我有多个查询返回。但是,我需要立即运行所有这些查询并将结果输出到excel文件。PHP - 运行多个查询将结果输出到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++; 
} 
+2

那么问题是什么? – ajtrichards

+4

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护,[弃用过程](http://j.mp/Rj2iVR)已经开始。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 –

+0

这是一个电子表格...你可以随时随地写信给任何一个单元格。您不必一次写一行。 –

回答

2

您可以尝试在PHP 5

http://us2.php.net/manual/en/mysqli.multi-query.php

除此之外,嵌套查询 “应该” 工作mysqli_multi_query。有可能你有错误的键/值元素被定位。如果不能访问正在使用的模式,我很难说。

但我同意,旧的功能将不推翻,所以需要更新更新的脚本方法。

<?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(); 
?> 
相关问题