0

我有一个 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 专家

4

2 回答 2

0
// Add &-sign to create a reference to $drinks
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))) 
    {
        $drink['servings'] = $serve;
    }
}

现在$row应该有所有的饮料,每种饮料都会有一个字段servings。要获取 JSON:

echo json_encode($row);
于 2013-10-19T06:36:53.793 回答
0

请不要在循环中使用 prepare(),这种方法非常慢,并且只准备一次查询。

// Add &-sign to create a reference to $drinks
$statement = $conn->prepare('SELECT * FROM servingTable WHERE id = :drinkId)');
$statement->bindParam(':drinkId' , $drinkId, PDO::PARAM_INT);
foreach($row as &$drink) {
    $drinkId  = $row->id;
    $statement->execute();//According to documentation

    if(($serve = $statement->fetchAll(PDO::FETCH_ASSOC))) 
    {
        $drink['servings'] = $serve;
    }
}

请参阅http://php.net/manual/en/pdo.prepared-statements.php (我也应该使用它,但它是由一个一对一的查询系统管理的,它需要改进)

于 2013-10-19T07:13:22.600 回答