作者:正好忍心_702 | 来源:互联网 | 2023-05-31 14:36
我使用MySQLi成功创建了一个android登录页面。现在,我想将其从MySQLi转换为PDO。当我将config.
我使用MySQLi成功创建了一个android登录页面。现在,我想将其从MySQLi转换为PDO。当我将config.php转换为PDO时,登录页面失败。下面我想显示成功登录的代码
config.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
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;
}
下面是我将config.php更改为PDO样式时的代码
configPDO.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();
}
?>
谁能知道是什么问题?是我的configPDO.php吗?