Android错误:java.lang.String类型的值<br不能转换为JSONObject

时间:2019-12-09 04:15:55

标签: php mysql pdo

当前,我创建具有登录功能的应用程序。为了从android连接到MySQL数据库,我使用PHP。当我使用MySQLi时,一切正常。但是当我转换为PDO时,该错误将与我的问题的标题相同。谁能知道这是什么问题吗?下面是我的PHP代码:

<?php 

    require_once 'configPDO.php';

    $response = array();

        if(isTheseParametersAvailable(array('badgeid', 'pwd'))){

            $badgeid = $_POST['badgeid'];
            $pwd = $_POST['pwd']; 

            $stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM users WHERE badgeid = :badgeid AND pwd = :pwd AND roles_id = 3");
            // $stmt->bind_param("ss",$badgeid, $pwd);

            $stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);
            $stmt->bindParam(':pwd',$pwd,PDO::PARAM_STR);
            $stmt->execute();

            //$stmt->store_result();

            if($stmt->rowCount() > 0){

                $stmt->bindParam($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{
                $response['error'] = false; 
                $response['message'] = 'Invalid username or password';
            }
        }
    echo json_encode($response);

    function isTheseParametersAvailable($params){

        foreach($params as $param){
            if(!isset($_POST[$param])){
                return false; 
            }
        }
        return true; 
    }

1 个答案:

答案 0 :(得分:2)

bindParam()条件内的第二个if(您应该阅读并理解此方法的确切作用!)是胡说八道!

更改此:

if($stmt->rowCount() > 0){

    $stmt->bindParam($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
    );

对此:

$result = $stmt->fetch(\PDO::FETCH_ASSOC); // Get results as array
if ($result) {
    // Since we only get the fields we want to send back, you can assign `$result` directly to `$response['user']`
     $response['user'] = $result; 

PHP引发了一个相关的错误,您将在请求的原始响应中看到该错误!