Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

php - Android Error: Value <br of type java.lang.String cannot be converted to JSONObject

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; 
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Change this:

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
    );

to this:

$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 had thrown an related error, which you would have seen in the raw response of you request!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...