如何使用Objective-c连接到我的php / MySQL服务器?

问题描述:

I have a MySQL database/table set up and a .php file online that inserts data into the mysql database. My php code is here:

<?

$name = $_GET['name'];
$minScore = $_GET['minScore'];
$hourScore = $_GET['hourScore'];
$dayScore = $_GET['dayScore'];

$service = "dddd.com";
$username = "dddd";
$password = "dddd";
$database = "dddd_DATA1";

mysql_connect($service, $username, $password);
@mysql_select_db($database) or die( "Unable to select database");

if($minScore>0) {
    $query="INSERT INTO currentScores VALUES ('$name', '$minScore', '$hourScore','$dayScore',null)";
    $result=mysql_query($query);
    echo("Score submitted! Name: $name, Score: $minScore");
}
else {
echo("No score was submitted");
}

?>

It works the way I want it to typing a URL link like this right in a browser: http://dddd.com/sendScores.php?name=NAMEYNAME&minScore=22&hourScore=3&dayScore=1

What I want to do now is somehow send this from my iPhone objective-c app. I assume I need to use NSURLRequests and NSURLConnections but I have been unable to get it to work so far. How can I do this? and if you have time... any ideas on how to get the info from the database back onto my app? Would I need to convert it to xml (or maybe a .plist) using php and then use an xml parser in objective-c? Thank you for your time.

我有一个MySQL数据库/表设置和一个.php文件在线,它将数据插入到mysql数据库中。 我的PHP代码在这里: p>

 &lt;?
 
 $ name = $ _GET ['name']; 
 $ minScore = $ _GET ['minScore']  ; 
 $ hourScore = $ _GET ['hourScore']; 
 $ dayScore = $ _GET ['dayScore']; 
 
 $ service =“dddd.com”; 
 $ username =“dddd”; \  n $ password =“dddd”; 
 $ database =“dddd_DATA1”; 
 
mysql_connect($ service,$ username,$ password); 
 @ mysql_select_db($ database)或die(“无法选择数据库”)  ; 
 
if($ minScore&gt; 0){
 $ query =“INSERT INTO currentScores VALUES('$ name','$ minScore','$ hourScore','$ dayScore',null)”; 
 $  result = mysql_query($ query); 
 echo(“得分提交!名称:$ name,得分:$ minScore”); 
} 
else {
echo(“未提交得分”); 
} 
  
?&gt; 
  code>  pre> 
 
 

它的工作方式我希望它在浏览器中输入这样的URL链接: http://dddd.com/sendScores.php?name=NAMEYNAME&minScore=22& hourScore = 3&amp; dayScore = 1 p>

我想做什么 现在以某种方式从我的iPhone objective-c app发送这个。 我假设我需要使用NSURLRequests和NSURLConnections,但到目前为止我还无法使用它。 我怎样才能做到这一点? 如果你有时间......有关如何从数据库获取信息回到我的应用程序的任何想法? 我需要使用php将其转换为xml(或者可能是.plist),然后在objective-c中使用xml解析器吗? 谢谢你的时间。 p> div>

I suggest using the ASIHTTPFramework which makes requesting URLs much easier. To provide a simple example with NSURLRequest you can do a synchronous request:

NSError * error = nil;
NSURLResponse * response = nil;
NSData * downloadedData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] 
                                                returningResponse:&response 
                                                            error:&error];

downloadedData contains the response body of the request, so the app received data you send along.

It is not recommended to use synchronous requests, cause they block your whole app.

The format to choose to send back data depends on the complexity. Use plain text if you just want to return a status code or some string. If you like to get a structured response with error code and an array or whatever, I suggest using JSON. XML is nice as well but using XMLParsers in iOS can be a pain. There are nice libraries to parse JSON, which is transformed to NSDictionaries and NSArrays and can therefore be easily used in your app.

I frequently make use of the json-framork