简体   繁体   中英

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. 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.

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. 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. 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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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