如何使用-initWithContentsOfURL获取MySQL查询结果?

时间:2021-08-29 07:51:21

I have a server-side php-script that fetches results from a MySQL table. I am trying to retrieve these results from within an iPhone OS app. I am using the following method:

我有一个服务器端的PHP脚本,从MySQL表中获取结果。我试图从iPhone OS应用程序中检索这些结果。我使用以下方法:

NSURL *myURL = [NSURL URLWithString:@"http://www.someurl.com/login.php?id=anid"];
NSArray *sqlResults = [[NSArray alloc] initWithContentsOfURL:myURL];

//Show me what went into the Array
NSLog(@"%@", [sqlResults ObjectAtIndex:0]);

*The NSLog at the bottom is me trying to see what just transpired. The NSLog statement is printing out (null) though -- so I am stuck. My problem is that I don't know what the heck I am trying to do with the NSArray of results and I am probably doing many things wrong. Is an internet connection implied (Have Cellular and Wi-fi)? So, my question is: What should I be doing to get MySQL query-results using -initWithContentsOfURL?

*底部的NSLog是我试图看到刚刚发生的事情。虽然NSLog语句打印出来(null)所以我被卡住了。我的问题是我不知道我试图用NSArray结果做什么,我可能做了很多错事。是否隐含了互联网连接(有蜂窝和Wi-Fi)?所以,我的问题是:我应该怎么做才能使用-initWithContentsOfURL来获取MySQL查询结果?

2 个解决方案

#1


In which format is your PHP script returning the MySQL results? It looks as though [NSArray initWithContentsOfURL:] requires the data to be in a very specific 'property list' format. Have you tried to see what output you get if you use NSString instead?:

您的PHP脚本以哪种格式返回MySQL结果?看起来好像[NSArray initWithContentsOfURL:]要求数据采用非常特定的“属性列表”格式。您是否尝试过查看使用NSString时获得的输出?:

[NSString initWithContentsOfURL:encoding:error:]

This should at least show you what you are getting out of your PHP script. Finally, take a take at the property list format. You may need to perform a transformation in your server side script or parse your PHP result set format on the iPhone as BJ Homer suggested.

这至少应该向您展示您从PHP脚本中获得的内容。最后,采取属性列表格式。您可能需要在服务器端脚本中执行转换,或者像BJ Homer建议的那样在iPhone上解析PHP结果集格式。

#2


According to the documentation on -[NSArray initWithContentsOfURL:] say;

根据文档 - [NSArray initWithContentsOfURL:]说;

The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects).

由aURL标识的位置处的数组表示必须仅包含属性列表对象(NSString,NSData,NSArray或NSDictionary对象)。

To my understanding, that means that it must be in plist format. If your results aren't in plist format, then try using -[NSString initWithContentsOfURL:] instead. If you then want to split it based on lines, you could call [downloadedString componentsSeparatedByString:@"\n"]

根据我的理解,这意味着它必须是plist格式。如果结果不是plist格式,请尝试使用 - [NSString initWithContentsOfURL:]。如果你想根据行拆分它,你可以调用[downloadedString componentsSeparatedByString:@“\ n”]

#1


In which format is your PHP script returning the MySQL results? It looks as though [NSArray initWithContentsOfURL:] requires the data to be in a very specific 'property list' format. Have you tried to see what output you get if you use NSString instead?:

您的PHP脚本以哪种格式返回MySQL结果?看起来好像[NSArray initWithContentsOfURL:]要求数据采用非常特定的“属性列表”格式。您是否尝试过查看使用NSString时获得的输出?:

[NSString initWithContentsOfURL:encoding:error:]

This should at least show you what you are getting out of your PHP script. Finally, take a take at the property list format. You may need to perform a transformation in your server side script or parse your PHP result set format on the iPhone as BJ Homer suggested.

这至少应该向您展示您从PHP脚本中获得的内容。最后,采取属性列表格式。您可能需要在服务器端脚本中执行转换,或者像BJ Homer建议的那样在iPhone上解析PHP结果集格式。

#2


According to the documentation on -[NSArray initWithContentsOfURL:] say;

根据文档 - [NSArray initWithContentsOfURL:]说;

The array representation at the location identified by aURL must contain only property list objects (NSString, NSData, NSArray, or NSDictionary objects).

由aURL标识的位置处的数组表示必须仅包含属性列表对象(NSString,NSData,NSArray或NSDictionary对象)。

To my understanding, that means that it must be in plist format. If your results aren't in plist format, then try using -[NSString initWithContentsOfURL:] instead. If you then want to split it based on lines, you could call [downloadedString componentsSeparatedByString:@"\n"]

根据我的理解,这意味着它必须是plist格式。如果结果不是plist格式,请尝试使用 - [NSString initWithContentsOfURL:]。如果你想根据行拆分它,你可以调用[downloadedString componentsSeparatedByString:@“\ n”]