PHP - MYSQL从数组中查询多个值

时间:2013-08-07 01:13:33

标签: php mysql sql arrays

我想在循环中运行插入查询,直到数组中的所有元素都通过。

例如:

$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次。

有人能帮助我吗?

1 个答案:

答案 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);
}