-4

When i run my code it gives me extra square brackets in Response, and i want to get response without these SQUARE Brackets.

Respose which i am getting is below. {"error":false,"message":"Login successfull","user":[{"u_id":"102","cust_name":"new","u_name":"user","cnic":"421","address":"sadaddd","cell_num":"43243","email":"n@gmail.com"}]}

Response which i want must be that: {"error":false,"message":"Login successfull","user":{"u_id":102,"cust_name":"new","u_name":"user","cnic":"421","address":"sadaddd","cell_num":"43243","email":"n@gmail.com"}}

My Code Section is below:

$user_login = $conn->prepare("SELECT u_id,cust_name,u_name,cnic,address,cell_num, email FROM users,cell_num WHERE u_name = :u_name AND password=:password AND users.u_id=cell_num.u_id_fk");
                        $user_login->execute(['u_name' => $u_name,'password'=>$password]); 


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

                            $row  = $user_login->fetchAll(\PDO::FETCH_ASSOC);

                            $response['user'] = $row; 
                            //echo $row;
                            //print_r($row);
  • 2
    passwords should never be stored as plain text, as we can not see the generation of $password i hoped they are hashed – nbk May 02 '20 at 13:37
  • As pointed out, plain text passwords are a big risk, have a look at https://stackoverflow.com/questions/30279321/how-to-use-phps-password-hash-to-hash-and-verify-passwords – Nigel Ren May 02 '20 at 13:41

1 Answers1

1

The square brackets means it is an array of data it's giving you rather than just one object.

Using fetchAll() will always return an array of rows matching the SELECT, if you know there is just one row, then you can use fetch() instead...

$row  = $user_login->fetch(\PDO::FETCH_ASSOC);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55