PDO插入不在循环内部工作

时间:2014-05-20 23:38:05

标签: php mysql loops pdo insert

我正在尝试使用PDO将我的Foursquare签到保存到MySQL数据库中。出于某种原因,此PDO插入代码在此循环中不起作用。我没有错误,也没有添加任何行...

假设我有一个我从Foursquare API中获取的对象:

$checkins = $users->response->checkins->items;

我正试图用它来做这件事:

try {

    $dbh = new PDO('mysql:host=localhost;dbname=nick', 'root', 'root'); 

    foreach( array_reverse($checkins) as $checkin ) {

        $sql = 'INSERT INTO foursquare (foursquare_id, checkin_date, venue_id, venue_name, venue_address, venue_city, venue_state, 
        venue_country, venue_zip, venue_lat, venue_lng, venue_category, icon, venue_url, been_here, shout) 

        VALUES (:foursquare_id, :checkin_date, :venue_id, :venue_name, :venue_address, :venue_city, :venue_state, 
        :venue_country, :venue_zip, :venue_lat, :venue_lng, :venue_category, :icon, :venue_url, :been_here, :shout)';

        $stmt = $dbh->prepare($sql);

        $stmt->execute(array(
            ':foursquare_id' => $checkin->id,
            ':checkin_date' => $checkin->createdAt,
            ':venue_id' => $checkin->venue->id,
            ':venue_name' => $checkin->venue->name,
            ':venue_address' => $checkin->venue->location->address,
            ':venue_city' => $checkin->venue->location->city,
            ':venue_state' => $checkin->location->state,
            ':venue_country' => $checkin->venue->country,
            ':venue_zip' => $checkin->venue->postalCode,
            ':venue_lat' => $checkin->venue->lat,
            ':venue_lng' => $checkin->venue->lng,
            ':venue_category' => $checkin->venue->categories[0]->name,
            ':icon' => $checkin->venue->categories[0]->icon->prefix.'32'.$checkin->venue->categories[0]->icon->suffix,
            ':venue_url' => $checkin->venue->url,
            ':been_here' => $checkin->venue->beenHere->count,
            ':shout' => $checkin->shout
            )
        );

        echo "<ul>";
        echo "<li>". $checkin->id . "</li>";
        echo "<li>". $checkin->createdAt . "</li>";
        echo "<li>". $checkin->venue->id . "</li>";
        echo "<li>". $checkin->venue->name . "</li>";
        echo "<li>". $checkin->venue->location->address . "</li>";
        echo "<li>". $checkin->venue->location->city . "</li>";
        echo "<li>". $checkin->venue->location->state . "</li>";
        echo "<li>". $checkin->venue->location->country . "</li>";
        echo "<li>". $checkin->venue->location->postalCode . "</li>";
        echo "<li>". $checkin->venue->location->lat . "</li>";
        echo "<li>". $checkin->venue->location->lng . "</li>";
        echo "<li>". $checkin->venue->categories[0]->name . "</li>";
        echo "<li>". $checkin->venue->categories[0]->icon->prefix.'32'.$checkin->venue->categories[0]->icon->suffix ."</li>";
        echo "<li>". $checkin->venue->url . "</li>";
        echo "<li>". $checkin->venue->beenHere->count . "</li>";
        echo "<li>". $checkin->shout . "</li>";
        echo "</ul><hr/>";

        echo $db->lastInsertId();

    }

} catch(PDOException $e) {

    echo $e->getMessage();

}

所有值在无序列表中都很好地回显。而我不明白的是,如果我尝试单独使用相同的代码,而不是循环,它会很好地插入值。像这样:

$dbh = new PDO('mysql:host=localhost;dbname=nick', 'root', 'root');

$sql = 'INSERT INTO foursquare (foursquare_id, checkin_date, venue_id, venue_name, venue_address, venue_city, venue_state, 
venue_country, venue_zip, venue_lat, venue_lng, venue_category, icon, venue_url, been_here, shout) 

VALUES (:foursquare_id, :datetime, :venue_id, :venue_name, :venue_address, :venue_city, :venue_state, 
:venue_country, :venue_zip, :venue_lat, :venue_lng, :venue_category, :icon, :venue_url, :been_here, :shout)';

$stmt->execute(array(
    ':foursquare_id' => '537834c8498e6ddd6b5cd649',
    ':datetime' => '1400386760',
    ':venue_id' => '50b55017e4b057a50b302ec0',
    ':venue_name' => 'ViewHouse Eatery, Bar & Rooftop',
    ':venue_address' => '2015 Market St',
    ':venue_city' => 'Denver',
    ':venue_state' => 'CO',
    ':venue_country' => 'United States',
    ':venue_zip' => '80205',
    ':venue_lat' => '39.7540584831',
    ':venue_lng' => '-104.993300193',
    ':venue_category' => 'American Restaurant',
    ':icon' => 'https://ss1.4sqi.net/img/categories_v2/food/default_32.png',
    ':venue_url' => 'http://www.viewhouse.com',
    ':been_here' => '1',
    ':shout' => ''
    )
);

我在这里做错了什么线索?

谢谢!

0 个答案:

没有答案