Answer json in c #

0

Json response with Postman:

    [
    {
        "id": "1",
        "correo": "[email protected]",
        "clave": "123456",
        "numero": "+1 5412-2154"
    }
]

Buton login:

try
            {

                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = "correo='" + txtuser.Text + "' and clave='" + txtpassword.Text + "'";
                byte[] DataBindings = encoding.GetBytes(postData);
                WebRequest request = WebRequest.Create("http://website.com/usuario_login.php");

                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = DataBindings.Length;

                Stream stream = request.GetRequestStream();
                stream.Write(DataBindings, 0, DataBindings.Length);
                stream.Close();

                WebResponse response = request.GetResponse();
                stream = response.GetResponseStream();

                StreamReader sr = new StreamReader(stream);
                String Mensaje = sr.ReadToEnd();
                if (Mensaje.Equals("null"))
                {
                    lblError.Text = "Al parecer los datos no coinciden";
                    btnlogin.Text = "Iniciar Sesión";
                }
                else
                {
                    voiceUrl admin = new voiceUrl(Mensaje);
                    this.Hide();
                    admin.ShowDialog();
                    this.Close();
                }
                sr.Close();
                stream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);

            }

Error:

  

Fatal error : Call to a member function fetch_assoc () on a non-object in

Php query file:

if($_SERVER["REQUEST_METHOD"]=="POST"){
include('conexion.php');
header('Content-Type: application/json; charset=utf8');
$correo= $_POST['correo'];
$clave= $_POST['clave'];  
$result  = mysqli_query($con,"SELECT * FROM usuarios WHERE correo='$correo' and clave='$clave'") or mysqli_error($con);
while ($row = $result->fetch_assoc()) {
     $arr[] = $row;
}

$json = json_encode($arr,JSON_UNESCAPED_UNICODE);
echo $json;
}

Does anyone know what is happening?

    
asked by DoubleM 08.02.2018 в 00:33
source

1 answer

0

100% sure that the result variable is a Boolean.

You have a logical operator in the line where you make the query:

$result  = mysqli_query($con,"SELECT * FROM usuarios WHERE correo='$correo' and clave='$clave'") or mysqli_error($con);

This "or" returns the result as a Boolean depending on whether you have an error (true) or if you have results.

Do a var_dump () to the result and you will see what kind of data it is.

    
answered by 10.02.2018 в 14:26