简体   繁体   English

使用PHP获取CSV导出(MYSQL)的列名和数据

[英]Use PHP to get Column Names and Data for CSV export (MYSQL)

I am in need of a way to export my MYSQL Database to CSV via PHP, but I need to select the column names as well. 我需要一种通过PHP将MYSQL数据库导出为CSV的方法,但是我还需要选择列名。 So Far I have the following which does everything I need except get the column names. 到目前为止,除了获得列名之外,我下面的所有工作都可以满足我的需要。

echo "Export Starting \n";
$SQL = ("SELECT *
FROM INF_TimeEntries
WHERE Exported IS NULL");
$result = mysqli_query($db_conn, $SQL) or die("Selection Error " . mysqli_error($db_conn));
echo "Export Data Selected \n";
$fp = fopen('../updateDatabase/timesheetExport/TimeEntries.csv', 'w');
echo "Starting Write to CSV \n";
while($row = mysqli_fetch_assoc($result)){
    fputcsv($fp, $row);
    $RowID = $row['ID'];
    $exportTime = date("Y-m-d H:i:s");
    $sql = ("UPDATE INF_TimeEntries
                    SET Exported = '$exportTime'
                    WHERE ID = '$RowID'");
    if ($mysqli_app->query($sql) === TRUE) {
    }
    else {
        echo date("Y-m-d H:i:s")."\n";
        echo "An Error Occured please contact the administrator ". $mysqli_app->error."\n";
    }
}
echo "Export Completed \n";
fclose($fp);
mysqli_close($mysqli_app);
mysqli_close($db_conn);

I am not sure how I would go about Achieving this. 我不确定如何实现这一目标。 I do not simply need to get column names but Column names and the data contained in each of these columns. 我不仅需要获取列名,还需要获取列名以及每个列中包含的数据。 I have not found any information on this in the other question suggested. 在另一个建议的问题中,我没有找到任何关于此的信息。

Since you're using mysqli_fetch_assoc , the name of the columns are the keys of the $row array in each iteration. 由于您使用的是mysqli_fetch_assoc ,因此列的名称是每次迭代中$row数组的键。 You can put that in the file in the first iteration: 您可以在第一次迭代中将其放入文件中:

echo "Starting Write to CSV \n";
$first = true;
while($row = mysqli_fetch_assoc($result)){
    if ($first) {
        fputcsv($fp, array_keys($row));
        $first = false;
    }
    fputcsv($fp, $row);
    // ..
}

Once you have your $result set from your mysqli_query() method, you can use mysqli_fetch_fields() to return an array of descriptions of the columns in the result set. 通过mysqli_query()方法设置$result ,可以使用mysqli_fetch_fields()返回结果集中各列的描述数组。

Each element of that array is a an object with several properties. 该数组的每个元素都是一个具有多个属性的对象。 One property is name -- which you can use as a header for your csv file. 一个属性是name -您可以将其用作csv文件的标题。 You also get properties like max_length , length , and table . 您还将获得诸如max_lengthlengthtable类的属性。 The linked documentation shows an example of using this metadata. 链接的文档显示了使用此元数据的示例。

This metadata is especially useful if you have a query more complex than SELECT * FROM table : if you assign aliases to the columns in your query, they show up in the name properties of the metadata array elements. 如果查询的内容比SELECT * FROM table更复杂,则此元数据特别有用:如果您为查询中的列分配别名,则别名将显示在元数据数组元素的name属性中。

This works even if the result set has no rows in it. 即使结果集中没有行,此方法也有效。

Sounds pretty simple, as long as everything else is already working for you. 听起来很简单,只要其他一切都已经为您服务。 You can create an array with the column names, and fputcsv($fp, $array_of_column_names) right before your while loop. 您可以在while循环之前使用列名称和fputcsv($fp, $array_of_column_names)创建一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM