gpt4 book ai didi

php - iOS 无法通过 PHP 在 iPhone 应用程序和 MySQL 数据库之间进行通信

转载 作者:行者123 更新时间:2023-11-29 04:31:17 24 4
gpt4 key购买 nike

我需要帮助尝试找出为什么我的 iPhone 应用程序无法与我的 PHP 通信。我有一个文本字段,用户可以在其中输入消息,并且有一个按钮应该告诉应用程序何时输入文本并需要将其发布到我的 mySQL 数据库。

下面粘贴了我的 PHP 和 iOS 代码,但我还在这里添加了一些调试语句。

我在 if([serverOutput...) 行上打了一个中断此时我得到以下信息:

alertsuccess    UIAlertView *   0x00000000dataURL NSData *    0x00000000 messageString   __NSCFString *  0x0685e7a0 @"hi"serverOutput    __NSCFConstantString *  0x00b75a7curl 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 代码

- (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];

}

最佳答案

这里出现错误$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())";

关于php - iOS 无法通过 PHP 在 iPhone 应用程序和 MySQL 数据库之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11715521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com