在php中使用多个值更新多行

时间:2014-04-06 10:10:33

标签: php mysql mysqli

我正在尝试使用多个值的数组更新同一列中的多个值,但我并不确切地知道为什么同一列中的值只使用数组中的一个值进行更新。

PHP

$subscribers = $db->query('SELECT * FROM subscribers');
while ($subscriber = $subscribers->fetch_object()) {
  $email = $subscriber->email;
}
$pagerank_update = $db->query('SELECT * FROM pagerank_update');
$url_pr = array('1','2','3','4');
foreach ($url_pr as $url_pr_value) {
 while ($website = $pagerank_update->fetch_object()) {
  $url = $website->URL; //domain1.com...
  $pagerank = $website->CURRENT_PR; // 0
  $date = $website->PR_TODAY; // 0
  $id = $website->ID;
  //$url_pr = getpr($url);
    //var_dump($url);
        //if ($url_pr_value !== $pagerank) {
     $db->query("UPDATE pagerank_update SET PR_TODAY = '$url_pr_value' WHERE URL = '$url'");
     $var = "UPDATE pagerank_update SET PR_TODAY = '$url_pr_value' WHERE URL = '$url'";
     var_dump($var);
    //} else {}
 }
}

var_dump用英语看起来像这样:

UPDATE pagerank_update SET PR_TODAY = '1' WHERE URL = 'http://domain1.com'
UPDATE pagerank_update SET PR_TODAY = '1' WHERE URL = 'http://domain2.com'
UPDATE pagerank_update SET PR_TODAY = '1' WHERE URL = 'http://domain3.com'
UPDATE pagerank_update SET PR_TODAY = '1' WHERE URL = 'http://domain4.com'

预期输出

UPDATE pagerank_update SET PR_TODAY = '1' WHERE URL = 'http://domain1.com'
UPDATE pagerank_update SET PR_TODAY = '2' WHERE URL = 'http://domain2.com'
UPDATE pagerank_update SET PR_TODAY = '3' WHERE URL = 'http://domain3.com'
UPDATE pagerank_update SET PR_TODAY = '4' WHERE URL = 'http://domain4.com'

我错在哪里?

1 个答案:

答案 0 :(得分:1)

试试这个:

$subscribers = $db->query('SELECT * FROM subscribers');
while ($subscriber = $subscribers->fetch_object()) {
  $email = $subscriber->email;
}
$pagerank_update = $db->query('SELECT * FROM pagerank_update');
$url_pr = array('1','2','3','4');
$i = 0;

while ($website = $pagerank_update->fetch_object()) {
  $url_pr_value = $url_pr[$i];
  $url = $website->URL; //domain1.com...
  $pagerank = $website->CURRENT_PR; // 0
  $date = $website->PR_TODAY; // 0
  $id = $website->ID;
  //$url_pr = getpr($url);
    //var_dump($url);
        //if ($url_pr_value !== $pagerank) {
     $db->query("UPDATE pagerank_update SET PR_TODAY = '$url_pr_value' WHERE URL = '$url'");
     $var = "UPDATE pagerank_update SET PR_TODAY = '$url_pr_value' WHERE URL = '$url'";
     var_dump($var);
    //} else {}
     $i++;
}