我使用PHP文件从MySQL到iPhone应用程序中获取数据,来说明我是不是意味着我在PHP请求和响应在iPhone上的PHP下降速度
<?php
$host = "localhost";
$user_name = "user_name";
$password = "password";
$db_name = "db_name";
$con = mysql_connect($host,$user_name,$password)or die (mysql_error());
mysql_select_db($db_name)or die (mysql_error());
mysql_query("set names utf8");
$SQL= "SELECT * FROM emails WHERE 1";
$RS = mysql_query($SQL) or die(mysql_error());
mysql_query("set character_set_server='utf8'");
while($row=mysql_fetch_assoc($RS))
$output[]=$row;
$json_encode =json_encode($output);
$utf8_decode = utf8_decode($json_encode);
echo $json_encode;
mb_convert_encoding($json_encode, 'UTF-8');
$html_entity_decode = html_entity_decode($json_encode);
?>
使用此代码并在iPhone我使用此代码:
NSString *phpUrl = @"url of my php file";
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
for (NSDictionary *status in statuses)
{
NSString *sEmailID = [status objectForKey:@"ID"];
NSString *sEmail = [status objectForKey:@"Email"];
NSString *sPassword = [status objectForKey:@"Password"];
NSString *sSMTP = [status objectForKey:@"SMTP"];
}
这对我工作正常,我得到了我的数据。我也在我的应用中多次使用这种方式。 但我的问题是请求和响应导致我的应用程序的速度降低,并使其工作如此缓慢。有什么方法可以降低请求和响应的速度吗?还是有其他方法可以替代这个吗? 在此先感谢。
请不要使用'mysql_ *'函数,因为它们在[deprecation process](http://news.php.net/php.internals/53799)中。改为使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php),[be一个更好的PHP开发人员](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –
好的,我会保留这一点。谢谢 – user1553381
您是否为每个请求创建/销毁与服务器的连接?因为这可能会导致大量开销并减慢速度。 – Sammitch