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

问题描述:

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

Currently, I create an apps with a login function. To connect from android to MySQL database, I use PHP. When I use MySQLi, everything is okay. But when I convert to PDO, The error will appear the same as my question's title. Can anyone knows what is the problem? Below is my PHP code:

<?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; 
    }

你的第二个 bindParam()(你应该阅读并理解这个方法到底是做什么的!)在 if代码>条件是胡说八道!

Your second bindParam() (You should read and understand what exactly this method do!) inside the if condition is nonsense!

改变这个:

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 抛出了一个相关错误,您会在请求的原始响应中看到!

PHP had thrown an related error, which you would have seen in the raw response of you request!