我有这个代码,它从MySQL数据库中检索用户的名字。但是,它不会加载我刚刚放入数据库的新条目。
-(void) fetchUsers {
@try
{
NSURL * url = [NSURL URLWithString:@"http://malaebna.com/usersMalaebna.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSMutableArray *jsonArray2 = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
preCalcArray = [[NSMutableArray alloc] init];
for (int i = 0; i < jsonArray2.count; i++) {
NSString *mEmail = [[jsonArray2 objectAtIndex:i] objectForKey:@"email"];
NSString *mPass = [[jsonArray2 objectAtIndex:i] objectForKey:@"password"];
NSString *mNumber = [[jsonArray2 objectAtIndex:i] objectForKey:@"number"];
NSString *mReservations = [[jsonArray2 objectAtIndex:i] objectForKey:@"reservations"];
NSLog(mEmail);
NSLog(mPass);
[preCalcArray addObject:[[users alloc] initWithEmail:mEmail andPassword:mPass andNumber:mNumber andReservations:mReservations]];
//[self.tableView reloadData];
}
}
@catch (NSException *ex) {
}
}
例如,我有一个创建帐户页面,我将创建一个帐户exampleemail@gmail.com。
它将成功插入数据库。
但是,当我返回登录页面并获取用户时,不会获取新条目。
我必须等待或刷新我的Chrome浏览器上的PHP页面才能显示在应用程序中。
这是我的php fetch脚本:
<?php
// Create connection
$con = new mysqli("localhost","","","");
$con ->set_charset("utf8");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM users";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray, JSON_UNESCAPED_UNICODE);
}
// Close connections
mysqli_close($con);
?>
我非常需要帮助。