dtkp51520
2013-08-07 01:13
浏览 132
已采纳

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

I want to run an insert query in a loop until all the elements from an array pass.

Ex:

$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);

I don't know how to run this query until all of $signs replaces $config['sign'] and the query runs all the 12 times in a loop.

Can somebody help me?

图片转代码服务由CSDN问答提供 功能建议

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

Ex:

  $ signs = array(“aries”,“taurus”,“gemini”,“cancer”,“leo”,“virgo”  ,“天秤座”,“天蝎座”,“射手座”,“摩羯座”,“水瓶座”,“双鱼座”); 
 
 
 $ config = array('sign'=>'aries',
' 输入'=>'每日',
'日期'=>日期('Ym-d'),); 
 
 
 $ content = $ horoscope-> get_daily_horoscope($ config); 
  $ update = $ db-> prepare(“INSERT INTO`horoscope`(`zodiacal_sign`,`last_updated`,`content`)值(%s,%s,%s)”,$ config ['sign'],  $ config ['date'],$ content); 
 $ db-> query($ update); 
   
 
 

我不知道如何运行 此查询直到所有 $ signs 替换 $ config ['sign'] ,并且查询在循环中运行所有12次。 < p>有人能帮帮我吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqby43944 2013-08-07 01:19
    已采纳

    You can do it like this..

    $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);
    }
    
    打赏 评论

相关推荐 更多相似问题