Breif query about reading from PDO

Codes here !

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 120
Joined: Tue Mar 01, 2016 3:07 pm

Thu Jun 21, 2018 6:18 am

Hello.

I'd just like to enquire if the coding to read all records in a database has changed?

Using PDO and php version 7, I'm now unable to read from a database part from the first record.

Code: Select all

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $conn->query('SELECT * FROM members');
    while ($row = $results->fetch()) {
     	// Do Something Here.
    }
}catch(PDOException  $e ){
    echo "<br>" . $e->getMessage();
}
Until quite recently, the above code would read each record in the database, but now it only reads the first record, and I'm unsure why.
With the above code, I'm in the process of re-writing the login screen.

Any ideas how to overcome this?
Thanks.

IChaps
New php-forum User
New php-forum User
Posts: 120
Joined: Tue Mar 01, 2016 3:07 pm

Thu Jun 21, 2018 8:46 am

Update:- Possible solution.


It's something to do with encryption/decryption.

I've discovered that when a function is called for the first time it works.
When it's called again it don't work.

Is there anyway is destroying the function after use?

Thanks.

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Thu Jun 21, 2018 9:16 pm

what function.

use $conn->fetchAll().

IChaps
New php-forum User
New php-forum User
Posts: 120
Joined: Tue Mar 01, 2016 3:07 pm

Fri Jun 22, 2018 2:35 am

Hello chorn,

At present, I've created a basic form signup form.
When the form is submitted, it is encrypted using the code below, and got the last ID number from the database (ie. $last_id = $conn->lastInsertId();)
Then I copied a png file from another location and renamed it to $last_id.png
Then I typed $_SESSION['photo_id'] = $last_id."png";

Finally just before I update the database, I would like to re-encrypt $_SESSION['photo_id'] which I can't seem to do.

Code: Select all

function encrypt_decrypt_test($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = "secret key a";
    $secret_iv = 'secret iv b';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    }
    else if( $action == 'decrypt' ){
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

$plain_txt = $_SESSION['photo_id'];
$encrypted_txt = encrypt_decrypt_test('encrypt', $plain_txt);
$_SESSION['photo_id'] = encrypt_decrypt_test;
Thanks.

Post Reply