我想在循环中运行插入查询,直到数组中的所有元素都通过。
例如:
$signs = array("aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces");
$config = array('sign' => 'aries',
'type' => 'daily',
'date' => date('Y-m-d'),);
$content = $horoscope->get_daily_horoscope($config);
$update = $db->prepare("INSERT INTO `horoscope` (`zodiacal_sign`, `last_updated`, `content`) values (%s,%s,%s)", $config['sign'], $config['date'], $content);
$db->query($update);
我不知道如何运行此查询,直到所有$signs
替换$config['sign']
并且查询在循环中全部运行12次。
有人能帮助我吗?
答案 0 :(得分:3)
你可以这样做..
$signs = array("aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces");
foreach($signs as $s){
$config = array('sign' => $s,
'type' => 'daily',
'date' => date('Y-m-d'),);
$content = $horoscope->get_daily_horoscope($config);
$update = $db->prepare("INSERT INTO `horoscope` (`zodiacal_sign`, `last_updated`, `content`) values (%s,%s,%s)", $config['sign'], $config['date'], $content);
$db->query($update);
}