我有一个 iPhone 代码如下
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *sqlSelectQuery = @"SELECT * FROM drinks";
// Query result
FMResultSet *results = [database executeQuery:sqlSelectQuery];
while([results next])
{
Drinks *drink = [[Drinks alloc] init];
[drink setDrinkId:[results intForColumn:@"id"]];
[drink setVenueId:[results intForColumn:@"venueId"]];
[drink setName:[results stringForColumn:@"name"]];
[drink setType:[results intForColumn:@"venueId"]];
NSMutableArray *servingSizes = [[NSMutableArray alloc] init];
NSString *sqlSelectQuery = [NSString stringWithFormat:@"SELECT * FROM servingSizes WHERE drinkId = %d", drink.drinkId];
FMResultSet *servingResult = [database executeQuery:sqlSelectQuery];
while([servingResult next])
{
Servings *serving = [[Servings alloc] init];
[serving setDrinkId:[servingResult intForColumn:@"drinkId"]];
[serving setServingId:[servingResult intForColumn:@"id"]];
[serving setServingSize:[servingResult intForColumn:@"servingSize"]];
[serving setPrice:[servingResult intForColumn:@"price"]];
[servingSizes addObject:serving];
}
[drink setServingsArray:servingSizes];
[drinksArray addObject:drink];
}
本质上,这段代码读取一个内置数据库并创建一个对象 DRINK,并从饮料的 id 获取表中的对象数组。
然而,这很容易,我想在 PHP 服务器端做以下事情。
这是我的代码
function GetAllDrinks()
{
try {
$conn = $this->GetDBConnection();
$statement = $conn->prepare('SELECT * FROM drinks');
$statement->execute();
if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC)))
{
return false;
}
foreach($row as $drink)
{
$drinkId = $row->id;
$statement = $conn->prepare('SELECT * FROM servingTable WHERE id = :drinkId)');
$statement->bindParam(':drinkId' , $drinkId, PDO::PARAM_INT);
$statement->execute();
if(($serve = $statement->fetchAll(PDO::FETCH_ASSOC)))
{
// WHAT TO DO HERE
}
}
$conn = null;
return $row;
} catch(PDOException $e) {
throw $e;
}
return $row;
}
现在我想为所有人创建一个 JSON 字符串,但我不确定如何在饮料对象中添加服务?我希望有人在这里提供帮助,因为我不是 PHP 专家