【问题标题】:php while mysql fetch array , update each 10 results at once ,php 而 mysql 获取数组,一次更新每 10 个结果,
【发布时间】:2015-11-17 10:39:51
【问题描述】:

我尝试使用 while mysql fetch array 从表中获取所有数据然后更新。 我使用我的代码来做到这一点,并且它工作正常,但是一个接一个。 我需要同时更新每 10 个结果,然后更新其他 10 个结果。

我的代码是

$get_urls = mysql_query("select * from urls where (status = 'active') ORDER BY number ASC");
    while($show_url = mysql_fetch_array($get_urls, MYSQL_NUM)) {
    $urlid = $show_url['0'];

    $edit_urls = "UPDATE urls SET online = 'yes' WHERE urlid = '$urlid'";
    mysql_query($edit_urls);

    }
    mysql_free_result($get_urls);

【问题讨论】:

  • 你的问题有点不清楚。你能修改一下你的语法并澄清你的问题吗?
  • 对不起我的英语不好。结果将是大约 3000 行,通常它会逐行更新。我需要一次更新每 10 行,然后再更新下 10 行等...

标签: php mysql loops while-loop


【解决方案1】:

你可以使用array & implode 来实现这个

$array = array();
$get_urls = mysql_query("select * from urls where (status = 'active') ORDER BY number ASC");
    while($show_url = mysql_fetch_array($get_urls, MYSQL_NUM))
       $array[] = $show_url['0'];

$edit_urls = "UPDATE urls SET online = 'yes' WHERE urlid IN (".implode(',',$array).")";
    mysql_query($edit_urls);

【讨论】:

    【解决方案2】:

    使用array_chunk

    $input_array = array('a', 'b', 'c', 'd');
    print_r(array_chunk($input_array, 2));
    //output  
    Array
    (
    [0] => Array
        (
            [0] => a
            [1] => b
        )
    [1] => Array
        (
            [0] => c
            [1] => d
        )
    )
    
    array_chunk($data,10);
    foreach ($data as $key => $value){
      // excute your sql query update. 
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多