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

mum43rcc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(244)

我有一个php页面,从sql表中选择记录,并将记录的加密版本发送到csv文件。我的问题是:当我选择没有使用openssl加密的记录时,它会成功导出,但是,如果我包含encrypt,它会打开一个空白屏幕。
我希望能在这里得到帮助。非常感谢

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);
        }
    }

    ?>

**

编辑:

**我已经编辑了部分代码,它现在可以导出记录到csv使用php openssl和工作良好,问题是,它添加了额外的列在csv文件。我想不出错误在哪里。以下是编辑后的代码:

<?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);
    }
}

?>
djmepvbi

djmepvbi1#

看起来您在执行查询的基础知识方面遇到了困难。我只需要编写一些简单的代码来获取和显示单个查询的结果。当你有了这个想法,你就可以为你的代码添加更多。
这是一个使用预处理语句的示例。这将阻止sql注入。您应该学习如何为以后的所有查询使用准备好的语句。这里有一个很好的资源链接,可以教你如何正确地进行任何查询。将此链接添加书签并经常引用。mysqli准备的声明
这段代码将从查询返回的结果的每一行打印出uniqueid。

$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>';

}

相关问题