如何从ios iphone sdk通过web服务(php)sql查询选择条件捕获数据
我有以下php文件:如何从ios iphone sdk通过web服务(php)sql查询选择条件捕获数据,iphone,mysql,objective-c,ios,web-services,Iphone,Mysql,Objective C,Ios,Web Services,我有以下php文件: <?php $username="root"; $database="testdb"; mysql_connect(localhost,$username); $user=$_GET["user"]; $password=$_GET["password"]; $query="SELECT documento FROM person WHERE user='".$user." and password ='".$password."'"; $result=mysq
<?php
$username="root";
$database="testdb";
mysql_connect(localhost,$username);
$user=$_GET["user"];
$password=$_GET["password"];
$query="SELECT documento FROM person WHERE user='".$user." and password ='".$password."'";
$result=mysql_query($query) or die (mysql_error("error "));
$num=mysql_numrows($result);
mysql_close();
$rows=array();
while($r=mysql_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>
现在,由于sql语句具有输入参数,因此输入参数不是代码,并尝试了类似的操作,但我无法满足需要。在字符串中传递参数。这里有一个例子
-(void) start {
NSString *user = [[NSString alloc] initWithString:@"exampleuser"];
NSString *password = [[NSString alloc] initWithString:@"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://myserver.com/myphpfile.php?user=%@?password=%@", user, password];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
因为它是密码和用户,所以通常最好使用POST而不是GET,因为这样它就不是可见URL的一部分。使用POST,如果您有https行,则可以隐藏它。但我认为这不是你现在主要关心的问题。上述方法应该有效。我对霍尔费德的代码做了一些修改,效果很好:)
}用户和密码不应该使用POST[]值吗?我对此不确定,但我很好奇。。
-(void) start {
NSString *user = [[NSString alloc] initWithString:@"exampleuser"];
NSString *password = [[NSString alloc] initWithString:@"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://myserver.com/myphpfile.php?user=%@?password=%@", user, password];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
-(void) start {
NSString *user = [[NSString alloc] initWithString:@"exampleuser"];
NSString *password = [[NSString alloc] initWithString:@"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://myserver.com/myphpfile.php?user=%@&password=%@", user, password];
NSString *urlstr_encoded = [urlstr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlstr_encoded];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];