2014-02-19 22 views
-2

您好我想從MySQL數據庫獲取值以顯示UITextField中的值。 這裏我的PHP編碼如何從使用php的mysql獲取值?

<?php 

$host = "localhost"; 
$user = "xcode"; 
$pass = "xcode"; 
$db="xcode"; 

$r = mysql_connect($host, $user, $pass); 

if (!$r) { 
    echo "Could not connect to server\n"; 
trigger_error(mysql_error(), E_USER_ERROR); 
} else { 
    echo "Connection established\n"; 
} 

echo mysql_get_server_info() . "\n"; 
$r2 = mysql_select_db($db); 

if (!$r2) { 
    echo "Cannot select database\n"; 
trigger_error(mysql_error(), E_USER_ERROR); 
} else { 
echo "database selected\n"; 
} 
$sql="select address,phone from login where name='{$_GET['name']}'"; 
if(!mysql_query($sql)) 
{ 
trigger_error(mysql_error(), E_USER_ERROR); 
} 
else 
{ 
    echo"1 record added"; 
} 
mysql_close(); 

?> 

這裏我的Xcode

- (IBAction)find:(id)sender { 
NSString *strURL=[NSString stringWithFormat:@"http://192.168.1.12:81/priya/sam.php?name=%@",name.text]; 



NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 

NSString *strResult=[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",strResult); 

}

我想在情節串連圖板來顯示文本字段中的地址和電話。

+0

你是否NSLog的狀態返回正確的值?然後你需要做的就是將它分配給UITextField,你可以爲它創建一個IBOutlet,並且設置UITextField出口的文本屬性,比如'self.myTextField.text = strResult' – rckoenes

+0

我得到了NSException –

+1

您不應該使用'mysql_' 其棄用! – Haudegen

回答

0

回答我的問題..

- (IBAction)find:(id)sender 
    { 


    NSError *err; 
    NSString *strURL=[NSString stringWithFormat:@"http://192.168.1.6:81/priya/sam.php?"]; 
    NSString *aa=name.text; 
    NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; 

    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataURL options: kNilOptions error:&err]; 


    NSMutableArray *array1=[[NSMutableArray alloc]init]; 
    array1=[jsonArray objectForKey:@"key"]; 
    NSLog(@"%@",array1); 


    for(int i=0;i<(array1.count);i++) 
    { 
    NSString *bb=[[array1 objectAtIndex:i]objectForKey:@"firstName"]; 
    if([bb isEqualToString:aa]) 
    { 
     address.text=[[array1 objectAtIndex:i]objectForKey:@"firstName"]; 
     phone.text=[[array1 objectAtIndex:i]objectForKey:@"lastname"]; 
    } 
    } 

    } 

感謝所有