How to load data from a specific mysql user in Xcode?

Asked

Viewed 181 times

0

My code is working, I’m getting the data I need, but I have to write the email of the user who wants the data in my php file. I wanted to receive user data that email is typed into a textField in my project. How could I do this?

My php file:

<?php

$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "*****";

// Criar conexao
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Queria pegar os dados do email que escrevi no textField, e para funcionar tenho que escrever o email diretamente no código   
    $sql = "SELECT id, nome, email, cpf, senha FROM usuario_log where email = '[email protected]'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $resultado = array("status" => "0", "id" => $row["id"], "nome" => $row["nome"], "email" => $row["email"], "cpf" => $row["cpf"], "senha" => $row["senha"]);
        }
    } else {
         echo "0 results";
    }
    echo json_encode($resultado);

        $conn->close();
?>

My file . m, I receive the data in my Log.

NSString *urlString = [NSString stringWithFormat: @"http://peggou.com.br/dados.php"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSString *statusUser = [json objectForKey:@"status"];
    NSString *idUser = [json objectForKey:@"id"];
    NSString *nomeUser = [json objectForKey:@"nome"];
    NSString *emailUser = [json objectForKey:@"email"];
    NSString *cpfUser = [json objectForKey:@"cpf"];
    NSString *senhaUser = [json objectForKey:@"senha"];
    NSLog(@"\n Status: %@,\n ID: %@,\n Nome: %@,\n E-mail: %@,\n CPF: %@,\n Senha: %@ \n", statusUser, idUser, nomeUser, emailUser, cpfUser, senhaUser);

1 answer

1

Try it this way:

NSData * dadosBrutos=[NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://peggou.com.br/dados.php"]];
id json = [NSJSONSerialization JSONObjectWithData:dadosBrutos options:NSJSONReadingMutableContainers error:NULL];
NSString *statusUser = [json objectForKey:@"status"];
NSString *idUser = [json objectForKey:@"id"];
NSString *nomeUser = [json objectForKey:@"nome"];
NSString *emailUser = [json objectForKey:@"email"];
NSString *cpfUser = [json objectForKey:@"cpf"];
NSString *senhaUser = [json objectForKey:@"senha"];
NSLog(@"\n Status: %@,\n ID: %@,\n Nome: %@,\n E-mail: %@,\n CPF: %@,\n Senha: %@ \n", statusUser, idUser, nomeUser, emailUser, cpfUser, senhaUser); 
  • Valew, but how do I pass the information from my textField to the php file, and how do I change my php so that it takes this information from my textfield to search, because in my current file I wrote the email directly in php to search, I wanted him to look for the email I wrote in my textfield

  • NSURL *url = [NSURL URLWithString:@"http://peggou.com.br/dados.php?email=%@", textfield.text];&#xA; NSData * dadosBrutos=[NSData dataWithContentsOfURL:url];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.