PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table otp ( phone varchar(64) primary key, otp int, type enum('new', 'old'), validity datetime ); insert into otp values ('+123456789', 1234, 'old', now()), ('+987654321', 4321, 'new', now());
Copy Clear
Copy Format Clear
<?php function generateOTP($theUser, $thePhone){ global $pdo; try { $stmt = $pdo->prepare(" INSERT INTO otp(phone, otp, type, validity) VALUES (:phone, :otp, :type, :val) ON DUPLICATE KEY UPDATE otp = :otp, type = :type, validity = :val"); $stmt-> bindValue(':phone', $thePhone); $stmt-> bindValue(':otp', rand(1000, 9999)); $stmt-> bindValue(':type', 'new'); $stmt-> bindValue(':val', date('Y-m-d H:i:s', strtotime('+5 mins'))); return $stmt-> execute(); } catch (PDOException $Exception ) { //print_r($Exception); return false; } } function sendOTP($theUser, $thePhone){ global $pdo; return generateOTP($theUser, $thePhone); } $theUser = 'User'; $thePhone = '+123456789'; // CALLED SOMEWHERE LIKE THIS if(sendOTP($theUser, $thePhone)) { echo "OTP SENT"; }else{ echo "OTP SENDING FAILED"; } $query = "SELECT * FROM otp;"; $stmt = $pdo->prepare($query); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); var_export($rows);
Show:  
Copy Clear