[英]iOS no communication between iPhone app and MySQL database via PHP
I need help trying to figure out why my iPhone app is not communicating with my PHP. 我需要帮助来弄清楚为什么我的iPhone应用程序无法与PHP通信。 I have a text field where the user can enter a message and there is a button that is supposed to tell the app when the text has been entered and needs to be posted to my mySQL database.
我有一个文本字段,用户可以在其中输入消息,并且有一个按钮可以告诉应用程序何时输入了文本,需要将其发布到mySQL数据库中。
My PHP and iOS code are pasted below, but I also included some debugging statements here. 我的PHP和iOS代码粘贴在下面,但是我在这里还包括一些调试语句。
I put a break at the line if([serverOutput...)
and at that point I get the following: 我在
if([serverOutput...)
行if([serverOutput...)
,到那时我得到了以下内容:
alertsuccess UIAlertView * 0x00000000 dataURL NSData * 0x00000000 messageString __NSCFString * 0x0685e7a0 @"hi" serverOutput __NSCFConstantString * 0x00b75a7c url NSURL * 0x06869540 @"http://www.mysite.com/connect.php?message=hi"
From the debug breakpoint, I would expect serverOutput to say "OK" but it's blank, which I believe means that it did not connect with my PHP. 从调试断点开始,我希望serverOutput会说“ OK”,但它为空,这意味着它未与我的PHP连接。 Your help is appreciated on how to fix this.
感谢您的帮助,以解决此问题。
<?php
// Connecting, selecting database
$link = mysql_connect("localhost", "user", "password")
or die('Could not connect: ' . mysql_error());
mysql_select_db("mydb") or die('Could not select database');
// Performing SQL query
$query = "INSERT INTO messages (message,date) VALUES ('$_GET["message"]',NOW())";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
iPhone code iPhone代码
- (IBAction)postmessage:(id)sender {
self.message = self.messageField.text;
NSString *messageString = self.message;
UIAlertView *alertsuccess;
//construct an URL for your script, containing the encoded text for parameter value
NSURL* url = [NSURL URLWithString:
[NSString stringWithFormat:
@"http://www.mysite.com/connect.php?message=%@",messageString]];
NSData *dataURL = [NSData dataWithContentsOfURL:url];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@"OK"]) {
alertsuccess = [[UIAlertView alloc] initWithTitle:@"Posted" message:@"Done"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
} else {
alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Done"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
}
[alertsuccess show];
}
Error is here $query = "INSERT INTO messages (message,date) VALUES ('$_GET["message"]',NOW())";
错误在这里
$query = "INSERT INTO messages (message,date) VALUES ('$_GET["message"]',NOW())";
Should be 应该
$query = "INSERT INTO messages (message,date) VALUES ('" . $_GET["message"] . "', NOW())";
or 要么
$query = "INSERT INTO messages (message,date) VALUES ('${_GET["message"]}', NOW())";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.