dongmei1911
2013-09-03 12:12
浏览 69
已采纳

preg_match_all数组插入mysql数据库循环不能正常工作

I'm trying to preg_match_all the first 12 digit number found in a mysql field, and insert that number in another field, looping through all results from the Select statement. I have the following, which puts only the first result it finds in all the fields, so every field3 gets the same 12 digit number inserted, that 12 digit number being from the lowest unique ID field found by the Select statement. Any help with this would be greatly appreciated.

<?php


$query = mysql_query("SELECT * FROM `dbase`.`table` WHERE field1 IS NOT NULL AND field3 IS NULL");
$regexp ='/[0-9]{12}/';

while ($row = mysql_fetch_assoc($query)){
$result=($row["field1"]);

preg_match_all($regexp,$result,$matches,PREG_PATTERN_ORDER);

foreach($matches[0] as $match => $where){
$id=($row["field2"]);
$sql="UPDATE `dbase `.`table ` set field3 = $where WHERE field2 = '$id'";

mysql_query($sql);
}
}
?>

I added

print_r( $matches );

and the output was an array with one result, the number being place in all the fields:

Array ( [0] => Array ( [0] => 290970571788 )

)

The curly bracket change fixed the print_r output to show the the list of 12 digit numbers in this format (only first two are shown): Array ( [0] => Array ( [0] => 151104658286 )

) Array ( [0] => Array ( [0] => 271249191324 )

) Thanks for that.

Utilizing the answers and suggestions given here, I've edited the code to show the final working version. The last big issue was getting the 2nd select statement to use the array result, properly looping and inserting each value in the proper row. Also cleaned up the 1st select statement and changed from preg_set_order to preg_pattern_order.

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

我正在尝试preg_match_all在mysql字段中找到的前12位数字,并将该数字插入另一个字段 ,循环遍历Select语句的所有结果。 我有以下内容,它只放置它在所有字段中找到的第一个结果,因此每个field3获得相同的12位数字插入,12位数字来自最低的唯一ID Select语句找到的字段。 任何帮助都将非常感激。

 &lt;?php 
 
 
 $ query = mysql_query(“SELECT * FROM`dbase` .table` WHERE  field1 IS NOT NULL AND field3 IS NULL“); 
 $ regexp ='/ [0-9] {12} /'; 
 
while($ row = mysql_fetch_assoc($ query)){
 $ result =(  $ row [“field1”]); 
 
 
nreg_match_all($ regexp,$ result,$ matches,PREG_PATTERN_ORDER); 
 
foreach($ match [0] as $ match =&gt; $ where){
 $ id  =($ row [“field2”]); 
 $ sql =“UPDATE`dbase` .`table`set field3 = $ where WHERE field2 ='$ id'”; 
 
mysql_query($ sql); 
  } 
} 
?&gt; 
   
 
 

我添加了

  print_r($ matches); 
    
 
 

输出是一个带有一个结果的数组,该数字放在所有字段中:

数组 (\ n [0] =&gt;数组 ( [0] =&gt; 290970571788 )

大括号更改 修复了print_r输出以显示此格式的12位数字列表(仅显示前两位): Array ( [0] =&gt;数组 ( [0] =&gt; 151104658286 )

) Array ( [0] =&gt;数组 ( [0] =&gt; 271249191324 ) \ n

) 谢谢。

利用此处给出的答案和建议,我编辑了代码以显示最终的工作版本。 最后一个大问题是让第二个select语句使用数组结果,正确地循环并在正确的行中插入每个值。 还清理了第一个select语句,并从preg_set_order更改为preg_pattern_order。

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

1条回答 默认 最新

  • dongyan2267 2013-09-03 12:18
    已采纳

    It shouldn't be

    {while ($row = mysql_fetch_assoc($query))
    
    }
    

    but

    while ($row = mysql_fetch_assoc($query)) {
    
    }
    

    You don't want to create code block with while statement but you want to provide code block for it.

    And also you're using same WHERE condition in SELECT and UPDATE so it'll update all selected rows with same value (all will match that condition).

    打赏 评论

相关推荐 更多相似问题