Purpose The purpose of this project is to demonstrate how to use the PDO with mySQL from PHP. I'll also show how you can encrypt and decrypt data with AES; if you need to generate some encryption/decryption keys, check out this simple password generator. Let's get started! |
$columnA = 1; $columnB = "somestring"; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("INSERT INTO tablename (columnA, columnB) values (:columnA, :columnB)"); // Assign and execute query $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->bindParam(':columnB', $columnB, PDO::PARAM_STR); $statement->execute(); } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;
$columnA = 1; $columnB = "somestring"; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $encrypt_key = "4ldetn43t4aed0ho10smhd1l"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("INSERT INTO tablename (columnA, columnB) values (:columnA, AES_ENCRYPT(:columnB, '$encrypt_key'))"); // Assign and execute query $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->bindParam(':columnB', $columnB, PDO::PARAM_STR); $statement->execute(); } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;
$rowNumber = 1; $columnA = 1; $columnB = "somestring"; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("UPDATE tablename SET columnA = :columnA, columnB = :columnB WHERE rowNumber = :rowNumber"); // Assign and execute query $statement->bindParam(':rowNumber', $rowNumber, PDO::PARAM_INT); $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->bindParam(':columnB', $columnB, PDO::PARAM_STR); $statement->execute(); } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;
$rowNumber = 1; $columnA = 1; $columnB = "somestring"; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $encrypt_key = "4ldetn43t4aed0ho10smhd1l"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("UPDATE tablename SET columnA = :columnA, columnB = AES_ENCRYPT(:columnB, '$encrypt_key') WHERE rowNumber = :rowNumber"); // Assign and execute query $statement->bindParam(':rowNumber', $rowNumber, PDO::PARAM_INT); $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->bindParam(':columnB', $columnB, PDO::PARAM_STR); $statement->execute(); } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;
$columnA = 1; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("SELECT columnA, columnB FROM tablename WHERE columnA = :columnA ORDER BY columnA ASC"); // Assign and execute query $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->setFetchMode(PDO::FETCH_ASSOC); $statement->execute(); // Get data while($row = $statement->fetch()) { $columnA_value = $row['columnA']; $columnB_value = $row['columnB']; } } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;
$columnA = 1; $mySQLDatabaseuser = "database user name created in mySQL"; $mySQLDatabasePassword = "database user password created in mySQL"; $mySQLDatabaseName = "name of your database"; $encrypt_key = "4ldetn43t4aed0ho10smhd1l"; $connDB = new PDO("mysql:host=localhost;dbname=$mySQLDatabaseName", $mySQLDatabaseuser, $mySQLDatabasePassword); $connDB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { // Query $statement = $connDB->prepare("SELECT columnA, AES_DECRYPT(columnB, '$encrypt_key') AS columnB FROM tablename WHERE columnA = :columnA ORDER BY columnA ASC"); // Assign and execute query $statement->bindParam(':columnA', $columnA, PDO::PARAM_INT); $statement->setFetchMode(PDO::FETCH_ASSOC); $statement->execute(); // Get data while($row = $statement->fetch()) { $columnA_value = $row['columnA']; $columnB_value = $row['columnB']; } } catch(PDOException $e) { print $e->getMessage(); } //Close the connection $connDB = null;