简体   繁体   English

从PHP中的mysql表将加密记录导出到csv文件失败

[英]Exporting encrypted record to csv file from mysql table in PHP fails

I have a PHP page that select record from my SQL table and send an encrypted version of the record to CSV file. 我有一个PHP页面,该页面从SQL表中选择记录,然后将记录的加密版本发送到CSV文件。 My problem is: when I select the record without using openssl encrypted, it export sucessfully, However, if I include the encrypte it opens a blank screen. 我的问题是:当我选择未使用openssl加密的记录时,它会成功导出,但是,如果我包含加密,它将打开一个空白屏幕。

i hope will get a help here. 我希望这里能有所帮助。 Many Thanks in advance 提前谢谢了

This is my code:

  <?php

    // Database Connection
    require("xxx.php");
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'))
    $encryption_key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';

     $result='select   *
            from encrypt';


            $file='grade/file'.date('YmdDH_i_s').'.csv';
            $qry = "select * from gb_centre where use_flag=1";
            $rs = mysqli_query($con,$qry);
            $getRowAssoc = mysqli_fetch_assoc($rs);
    $query = (
        openssl_encrypt($result->uniqueid,'AES-256-CBC',$encryption_key,0,$iv),
        openssl_encrypt($result->username,'AES-256-CBC',$encryption_key,0,$iv),
    openssl_encrypt($result->shortname,'AES-256-CBC',$encryption_key,0,$iv),
    openssl_encrypt($result->rawgrade,'AES-256-CBC',$encryption_key,0,$iv),
    openssl_encrypt($result->finalgrade,'AES-256-CBC',$encryption_key,0,$iv),
    openssl_encrypt($result->cid,'AES-256-CBC',$encryption_key,0,$iv),
    openssl_encrypt($result->id,'AES-256-CBC',$encryption_key,0,$iv)

    );


    if (!$result = mysqli_query($con, $query)) {
        exit(mysqli_error($con));
    }

    $users = array($uniqueid,$username,$shortname,$rawgrade,$finalgrade,$cid,$id);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $users[] = $row;
        }
    }

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename='.$getRowAssoc['cid'].date('_Y-m-d_Hi').'.csv');
    $output = fopen('php://output', 'w');
    fputcsv($output, array('a1', 'a2', 'a3', 'a4', 'a5','a6','a7'));

    if (count($users) > 0) {
        foreach ($users as $row) {
            fputcsv($output, $row);
        }
    }

    ?>

** **

EDIT : 编辑

** I have edited the section of the code, it can now export the record to csv using the PHP OpenSSL and works fine, The problem is, it adds extra columns in the CSV file. **我已经编辑了代码部分,它现在可以使用PHP OpenSSL将记录导出到csv并可以正常工作,问题是,它在CSV文件中添加了额外的列。 I can't figure out where the error is. 我不知道错误在哪里。 Here is the edited code: 这是编辑后的代码:

<?php
// Database Connection
require("config.php");
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';
$encryption_key = 'bRuD5WYw5wd0rdHR9yLlM6wt2vteuiniQBqE70nAuhU=';
$string='select   *
        from encrypt'
$qry = "select * from gb_centre where use_flag=1";
        $rs = mysqli_query($con,$qry);
        $getRowAssoc = mysqli_fetch_assoc($rs);
if (!$result = mysqli_query($con, $string)) {
    exit(mysqli_error($con));
}


if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

        $uniqueid[] = openssl_encrypt($row->uniqueid,'AES-256-CBC',$encryption_key,0,$iv);
    $username[] = openssl_encrypt($row->username,'AES-256-CBC',$encryption_key,0,$iv);
    $shortname[] = openssl_encrypt($row->shortname,'AES-256-CBC',$encryption_key,0,$iv);
    $rawgrade[] = openssl_encrypt($row->rawgrade,'AES-256-CBC',$encryption_key,0,$iv);
    $finalgrade[] = openssl_encrypt($row->finalgrade,'AES-256-CBC',$encryption_key,0,$iv);
    $cid[] = openssl_encrypt($row->cid,'AES-256-CBC',$encryption_key,0,$iv);
    $id[] = openssl_encrypt($row->id,'AES-256-CBC',$encryption_key,0,$iv);
    }
}
$users = array($uniqueid,$username,$shortname,$rawgrade,$finalgrade,$cid,$i);
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$getRowAssoc['cid'].date('_Y-m-d_Hi').'.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('a1', 'a2', 'a3', 'a4', 'a5','a6','a7'));

if (count($users) > 0) {
    foreach ($users as $row1) {
        fputcsv($output, $row1);
    }
}

?>

It looks like you are having a hard time with the fundamentals of executing queries. 看起来您在执行查询的基础方面遇到了困难。 I would just write some simple code to fetch and show the results of a single query. 我只是编写一些简单的代码来获取并显示单个查询的结果。 Then when you get the idea you can add more to your code. 然后,当您有了主意时,可以在代码中添加更多内容。

This is an example using prepared statements. 这是一个使用预准备语句的示例。 This will prevent sql injections. 这将防止sql注入。 You should learn how to use prepared statements for all of your queries going forward. 您应该学习如何将准备好的语句用于以后的所有查询。 Here is a link to a good resource that will teach you how to do any query you need to do properly. 这里是一个很好的资源链接,它将教您如何正确执行所需的任何查询。 Bookmark this link and refer to it often. 将此链接添加为书签并经常引用。 MySQLi Prepared Statements MySQLi预备语句

This code will print out the uniqueid from each row of the results returned from your query. 此代码将从查询返回的结果的每一行中打印出唯一标识。

$query = "SELECT * 
FROM gb_centre 
WHERE use_flag = ?";

$stmt = $con->prepare($query);
$stmt->bind_param('i', 1); //set your bindings
$stmt->execute();
while ($result = $stmt->fetch_object()) {

  echo $result->uniqueid . '<br>';

}

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

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