-1

I'm having trouble comparing the value returned from a get request to a variable. The get returns in a format that includes the row in the databases name along with the value. Since the variable does not have that "tag" too they don't compare correctly. Is there a way to compare them properly?

{"email":"[value]"}

Thats the format its returning in. The value is in an array.

$app->get('/api/user_info/{email}', function($request, $response) {

    require_once('dbconnect.php');
    $email = $request->getAttribute('email');
    $query = "select email from user_info";
    $result = $mysqli->query($query);
    $count = 0;
    while($row = $result->fetch_assoc()){
        $data[] = $row;
    }
    for ($i=0; $i < count($data); $i++){
        if($data[$i] == ($email)){
            $newResponse = $response->withjson($data[$i]);
            return $newResponse;
            //Should return email if they're the same
        }
        else {
            $count += 1;
        }
        if ($count == count($data)){
            $newResponse = $response->withjson("Email not found");
            return $newResponse;
        }
    }  
}); 
Hxzzl
  • 1
  • 1
  • That's the format *what*, specifically, is returning in? – Stephen R Apr 30 '21 at 22:03
  • 1
    The whole function is utterly bad. `SQL` supports a `WHERE` clause (but it needs precautions regarding [SQL Injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)). – maio290 Apr 30 '21 at 22:08
  • In the browser thats what shows up {"email":"someEmail@gmail.com"} – Hxzzl Apr 30 '21 at 22:10
  • 1
    Heh. I didn't even look that deep into what you were doing. @maio290 is right -- put a WHERE clause in your SQL rather than returning the entire table column and parsing after – Stephen R Apr 30 '21 at 22:13
  • Haha, yea that's wayyyy better. I changed my plan and tried to implement it through the old way instead of rewriting. Appreciate it! – Hxzzl Apr 30 '21 at 22:18

1 Answers1

0

json_decode( '{"email":"[value]"}' ) will give you a standard PHP object with an "email" property.

I'm guessing a bit as to what GET key to use, but this should help you figure it out:

$var = json_decode( $_GET['email'] );
echo $var->email;
Stephen R
  • 3,512
  • 1
  • 28
  • 45
  • Sorry if I was a little unclear, the returning of $data[i] is in that format on the frontend therefore I have no access to it in that format in the backend. And it's an array so json_decode won't work. – Hxzzl Apr 30 '21 at 22:08
  • I understood your question to say $_GET returned the string `{"email":"[value]"}`. Could you perhaps clarify by editing the question to show the `print_r` output of the value you're talking about? We can't help if you're not specific – Stephen R Apr 30 '21 at 22:10