-1

I made a request from MySQL and my code below is not working:

$email =$_POST['email'];  
$parola =$_POST['parola'];

$rez = mysqli_query($con, "SELECT * FROM utilizatori WHERE email='$email' AND parola='$parola'");

if($rez){
    $succes = 1;
} else {
   $succes = 0;
}

$data = array("succes" => $succes);
echo json_encode($data);

In postman it shows me this:
enter image description here

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Cosmin Ciolacu
  • 406
  • 9
  • 29
  • Which means your query failed, please echo `mysqli_error($con)` to see the error message. – catcon Sep 16 '19 at 10:24
  • where is data variable? –  Sep 16 '19 at 10:25
  • 1
    do `$rez = mysqli_query($con, "SELECT * FROM utilizatori WHERE email='$email' AND parola='$parola'") or die(mysqli_error($con));` and check error – Alive to die - Anant Sep 16 '19 at 10:26
  • 1
    Also your SQL is open to SQL injection, please use prepare statement for better security – catcon Sep 16 '19 at 10:26
  • See: https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die – Dharman Sep 16 '19 at 10:30
  • This output is correct because your code return, $data = array("succes" => $succes); json_encode($data); – Casper Sep 16 '19 at 10:34
  • Use mysqli_fetch_assoc function to fetch result as an associative array, then combine with $data array. $row = mysqli_fetch_assoc($rez); $data = array("succes" => $succes, "data" => $row); – Casper Sep 16 '19 at 10:42

1 Answers1

0

You missed the data variable or your query doesn't not return any output. Please try to use the MySQL query in Phpmyadmin or CLI. If it is OK then use the code like :

$email =$_POST['email'];  
$parola =$_POST['parola'];

$rez = mysqli_query($con, "SELECT * FROM utilizatori WHERE email='$email' AND parola='$parola'");

while($result = mysqli_fetch_array($rez))
{
 $data[] = $result;

}
if(mysqli_num_rows($rez) != 0) {
    $data = array("success" => "Success", "data" => $data );
} else {
    $data = array("success" => "Failed", "data" => array() );
}

echo json_encode($data);
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98