I successfully created an android login page using MySQLi. Now, I want to convert it from MySQLi to PDO. When I convert the config.php to PDO, the login page is unsuccessful. Below I want to show the code of successful login
config.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ot_worker');
$conn = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
login.php
<?php
require_once 'configPDO.php';
//an array to display response
$response = array();
//for login we need the username and password
if(isTheseParametersAvailable(array('badgeid', 'pwd'))){
//getting values
$badgeid = $_POST['badgeid'];
$pwd = $_POST['pwd'];
//creating the query
$stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM users WHERE badgeid = ? AND pwd = ? AND roles_id = 2");
$stmt->bind_param("ss",$badgeid, $pwd);
$stmt->execute();
$stmt->store_result();
//if the user exist with given credentials
if($stmt->num_rows > 0){
$stmt->bind_result($badgeid, $email, $fullname, $roles_id, $team_id);
$stmt->fetch();
$user = array(
'badgeid'=>$badgeid,
'email'=>$email,
'fullname'=>$fullname,
'roles_id'=>$roles_id,
'team_id'=>$team_id
);
$response['error'] = false;
$response['message'] = 'Login successfull';
$response['user'] = $user;
}else{
//if the user not found
$response['error'] = false;
$response['message'] = 'Invalid username or password';
}
}
//displaying the response in json structure
echo json_encode($response);
//function validating all the paramters are available
//we will pass the required parameters to this function
function isTheseParametersAvailable($params){
//traversing through all the parameters
foreach($params as $param){
//if the paramter is not available
if(!isset($_POST[$param])){
//return false
return false;
}
}
//return true if every param is available
return true;
}
below is the code when I change config.php to PDO style
configPDO.php
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'ot_worker';
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("CREATE DATABASE IF NOT EXISTS `$dbname`");
$conn->exec("use `$dbname`");
}
catch(PDOException $error) {
$error->getMessage();
}
?>
Can anyone know what is the problem? is that at my configPDO.php?