Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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表组合中的数据导出为Excel_Php_Mysql_Excel - Fatal编程技术网

Php 将MySQL表组合中的数据导出为Excel

Php 将MySQL表组合中的数据导出为Excel,php,mysql,excel,Php,Mysql,Excel,我知道如何将MySQL表导出为Excel。但我的问题是,我有一个复杂的场景,可以从多个MySQL表中获取数据,并将它们排列到HTML表输出中。我的问题是:如何将这些数据输出为Excel 这是我的密码 <?php include(".../dbconnect.txt"); $result = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error()); ?&g

我知道如何将MySQL表导出为Excel。但我的问题是,我有一个复杂的场景,可以从多个MySQL表中获取数据,并将它们排列到HTML表输出中。我的问题是:如何将这些数据输出为Excel

这是我的密码

<?php 
include(".../dbconnect.txt");

$result = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error());
?>

<table width='1450'>
<tr>
<td>S/N</td>
<td>Job Title</td>
<td>Applicant</td>
<td>Date</td>
<td>Expe</td>
<td>Course</td>
<td>Email</td>
<td>Quali</td>
<td>Age</td>
<td>Gender</td>
</tr>
<?php
$cnt =1;
while($row = mysql_fetch_array($result)){
$count = $cnt; $cnt++;
?>
<tr>
<td>
<?php echo $count; ?>
</td>
<td>
<?php
$re = mysql_query("select * from table1 where id='".$_GET['jobid']."'") or die(mysql_error());
$rw = mysql_fetch_array($re);
echo $rw['title'];
?>
</td>
<td> 
<?php
$re1 = mysql_query("select * from table2 where email='".$row['email']."'") or die(mysql_error());
$rw1 = mysql_fetch_array($re1);

echo ucwords($rw1['fname']); 
echo "&nbsp;";
echo ucwords($rw1['lname']);
?>
</td>
<td><?php echo $row['date']; ?></td>
<td>
<?php 
$re2 = mysql_query("select * from table3 where email='".$row['email']."'") or     die(mysql_error());
$rw2 = mysql_fetch_array($re2);
echo $rw2['years'];
?>
</td>
<td>
<?php 
$re3 = mysql_query("select * from table4 where email='".$row['email']."'") or die(mysql_error());
$rw3 = mysql_fetch_array($re3);
echo ucwords($rw3['course']);
?>
</td>
<td>
<?php echo $row['email']; ?>
</td>
<td> 
<?php 
echo $rw3['quali'];
?>
</td>
<td>
<?php echo $rw1['age']; ?>
</td>
<td>
<?php echo $rw1['gender']; ?>
</td>
</tr>
<?php } ?>
</table>

如何在Excel中输出此表?

这是一个如何将所有查询转换为一个漂亮数组的示例,该数组可以传递到html或Excel。过了一段时间我觉得很无聊,所以我只输入了几个问题。但是你应该可以从这里开始

$rows = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error());
$items = array();

while($row = mysql_fetch_array($result))
{
    $item = array();

    // Master stuff
    $item['date'] = $row['date'];

    // Title
    $re = mysql_query("select * from table1 where id='".$_GET['jobid']."'") or die(mysql_error());
    $rw = mysql_fetch_array($re);
    $item['title'] = $rw['title'];

    // Names
    $re1 = mysql_query("select * from table2 where email='".$row['email']."'") or die(mysql_error());
    $rw1 = mysql_fetch_array($re1);

    $item['name'] = ucwords($rw1['fname']) . ' ' . ucwords($rw1['lname']);

    // ... rest of queries

    // Add to list
    $items[] = $item;
}

我建议您稍微熟悉一下sql左连接操作。您可以大大减少查询的数量。甚至可能只有一个。

使用许多类似的库中的一个,可以在初始查询后立即编写Excel文件,循环查询结果,执行所有其他子查询,并将所有内容存储在一个数组中。现在,可以将数组传递给html模板(您需要调整它以使用数组)或excel生成器。你也会得到更容易阅读的代码。谢谢大家。我已经解决了。
$rows = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error());
$items = array();

while($row = mysql_fetch_array($result))
{
    $item = array();

    // Master stuff
    $item['date'] = $row['date'];

    // Title
    $re = mysql_query("select * from table1 where id='".$_GET['jobid']."'") or die(mysql_error());
    $rw = mysql_fetch_array($re);
    $item['title'] = $rw['title'];

    // Names
    $re1 = mysql_query("select * from table2 where email='".$row['email']."'") or die(mysql_error());
    $rw1 = mysql_fetch_array($re1);

    $item['name'] = ucwords($rw1['fname']) . ' ' . ucwords($rw1['lname']);

    // ... rest of queries

    // Add to list
    $items[] = $item;
}