热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

当PHPMySQLi更改为PDO时,Android登录失败

我使用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吗?





推荐阅读
author-avatar
正好忍心_702
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有