Home > Software design >  get values of sql return php
get values of sql return php

Time:12-03

I have this model

class LoginModel extends Database
{
    public function signIn($userName,$password){
        return $this->select("select count(*) as o_user_exists from users where usuario = ? and password =  ?");                    
    }
}
?> 

My function select

public function select($query = "" , $params = [])
{
    try {
        $stmt = $this->executeStatement( $query , $params );
        $result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);             
        $stmt->close();

        return $result;
    } catch(Exception $e) {
        throw New Exception( $e->getMessage() );
    }
    return false;
}

and in my controller i have

$userModel = new LoginModel();            
 $actionSignIn = $userModel->signIn($p_userName,$p_password);        
 $responseData = json_encode($actionSignIn[0],true); 
 echo $responseData->o_user_exists;

enter image description here

how i can get values from this return?

{"o_user_exists":1}

i edit my Code to

 $userModel = new LoginModel();            
                $actionSignIn = $userModel->signIn($p_userName,$p_password);                                        
                $responseData = json_decode(json_encode($actionSignIn),true);                 
                echo $responseData['o_user_exists'];

and now y get Undefined index
enter image description here

CodePudding user response:

You are getting an JSON object as a result. You can json_decode and access it as an array.

$responseArray = json_decode($response, true);
$responseArray['o_user_exists'];
  • Related