iOS 没有通过 PHP 在 iPhone 应用程序和 MySQL 数据库之间进行通信 [英] iOS no communication between iPhone app and MySQL database via PHP
问题描述
我需要帮助试图找出我的 iPhone 应用程序无法与我的 PHP 通信的原因.我有一个文本字段,用户可以在其中输入消息,并且有一个按钮应该告诉应用程序何时输入文本并需要将其发布到我的 mySQL 数据库.
我的 PHP 和 iOS 代码粘贴在下面,但我也在此处包含了一些调试语句.
我在 if([serverOutput...)
行中断,然后我得到以下信息:
从调试断点来看,我希望 serverOutput 说OK",但它是空白的,我相信这意味着它没有与我的 PHP 连接.感谢您提供有关如何解决此问题的帮助.
iPhone 代码
- (IBAction)postmessage:(id)sender {self.message = self.messageField.text;NSString *messageString = self.message;UIAlertView *alertsuccess;//为您的脚本构建一个 URL,包含参数值的编码文本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];} 别的 {alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Done"delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];}【提示成功显示】;}
Error is here $query = "INSERT INTO messages (message,date) VALUES ('$_GET["message"]',NOW())";
应该
$query = "INSERT INTO messages (message,date) VALUES ('" . $_GET["message"] . "', NOW())";
或
$query = "INSERT INTO messages (message,date) VALUES ('${_GET["message"]}', NOW())";
I need help trying to figure out why my iPhone app is not communicating with my 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.
My PHP and iOS code are pasted below, but I also included some debugging statements here.
I put a break at the line if([serverOutput...)
and at that point I get the following:
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. 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
- (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())";
Should be
$query = "INSERT INTO messages (message,date) VALUES ('" . $_GET["message"] . "', NOW())";
or
$query = "INSERT INTO messages (message,date) VALUES ('${_GET["message"]}', NOW())";
这篇关于iOS 没有通过 PHP 在 iPhone 应用程序和 MySQL 数据库之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!