How to use a user’s information in the mysql database on a label? iOS

Asked

Viewed 142 times

0

I’m developing an iOS app that connects to the mysql database and logs in the user using the email and password information, this part is working well. Now I’m wanting to take other information from this user and display it on a label. How would I do that

My PHP file:

<?php
// Ghalia ALrajban
// 21 Dec 2011

if (isset($_GET["email"])  && isset($_GET["senha"]) ){
                $email = $_GET["email"];
                $senha = $_GET["senha"];
                $result = login( $email, $senha);
                echo $result;
                }

function makeSqlConnection()
{
$DB_HostName = "dbmy0012.whservidor.com";
$DB_Name = "peggou";
$DB_User = "peggou";
$DB_Pass = "peg2015tec";

    $con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 

        mysql_select_db($DB_Name,$con) or die(mysql_error()); 

    return $con;
}

function disconnectSqlConnection($con)
{
    mysql_close($con);
}

function login($email, $senha)
{
    //require (FILE);
    $con = makeSqlConnection();

    $sql = "select * from usuario_log  where email = '$email' and senha = '$senha';";
    $res = mysql_query($sql,$con) or die(mysql_error());

    $res1 = mysql_num_rows($res);

    disconnectSqlConnection($con);

     if ($res1 != 0) {
        return 1;
    }else{
        return 0;
    }// end else


}// end of Function 

?>

My Boot Login in . m file:

    #pragma mark - BOTAO LOGIN
- (IBAction)loginButton:(id)sender {
    // Verificar e informar se os dados estao em branco.
    if([emailTextField.text isEqualToString:@""] || [senhaTextField.text isEqualToString:@""]) {
        UIAlertView *errorbranco = [[UIAlertView alloc] initWithTitle:@"Erro" message:@"Preencha todos os campos" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [errorbranco show];
    }

    //Verificar informacoes no banco de dados
    else{
        NSString *strURL = [NSString stringWithFormat:@"http://peggou.com.br/login.php?email=%@&senha=%@", emailTextField.text, senhaTextField.text];
        NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
        NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

        //Informacoes Corretas
        if ([strResult isEqualToString:@"1"]) {
            [self performSegueWithIdentifier:@"entrou" sender:self];
        }
        //Informacoes Incorretas
        else{
            UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Erro" message:@"e-mail ou senha invalidos" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [error show];
        }
    }
}

1 answer

0

I understood what you are trying to do, the problem is that both the php file and the Ios file do not have this information.

This code piece returns only true or false, or rather, your code is checking if you found any record in the database. It would be the case to rethink, and do something of the type I put below, returning a json that contains an array of the result that was found in the database.

Previously I was only returning the number of records of the database, if it was equal to 1 I said that I found the user, then I said I had logged in.

function login($email, $senha)
{

    $con = makeSqlConnection();

    $sql = "select * from usuario_log  where email = '$email' and senha = '$senha';";
    $res = mysql_query($sql,$con) or die(mysql_error());

    disconnectSqlConnection($con);

    // Aqui retornaria um json contendo o array das informações do usuário.
    echo json_encode($res);
}

Now it would be necessary to interpret the json that was sent by php, getting the user information that logged in.

OBS: It would be interesting to rethink the application a little and pass these login parameters via POST, and not GET request (passing the parameters via url).

  • Ola Lucas, thanks for your help, I tried to interpret json as follows: NSString *strURL = [NSString stringWithFormat:@"http://peggou.com.br/dados.php?email=%@&senha=%@", email, senha];&#xA; NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];&#xA; NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];&#xA; NSLog(@"%@",strResult);&#xA; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataURL options:kNilOptions error:nil];&#xA; NSLog(@"%@",json);&#xA; idUser = [json objectForKey:@"id"];&#xA; NSLog(@"%@",idUser);

  • I received the json information as null.

  • Fernando, do the following, try to access this php data by the browser to know what it is returning from the database. Maybe it’s the format he’s returning.

  • Hello Lucas, when I type the email I want to search directly in php it gives the correct information, it does not play the information when I try to use the login information sent from the app

Browser other questions tagged

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