+ -
当前位置:首页 → 问答吧 → php 遍历 更新 求教! 在线等、

php 遍历 更新 求教! 在线等、

时间:2011-11-10

来源:互联网

foreach($arr_hang as $lie){

$lie 有很多个内容、


$rs=mysql_query("select * from `user_list` where `username`='$lie'"); 
$row=mysql_fetch_array($rs);
echo $ext1=$row['ext1']+2500;
$data=mysql_query("update `user_list` set `ext1`='$ext1' where `username`='$lie'");

}
目前只更新最后一个内容。想更新所有 $lie

作者: qqmnme   发布时间: 2011-11-10

PHP code
foreach( $arr_hang as $lie ){
mysql_query("update `user_list` set ext1 = ext1 + 2500  where `username`='$lie'" );
if( mysql_affected_rows >= 0 )
     echo "更新成功:".$lie;
else
     echo "ERROR:".mysql_error();
} 

纳闷的是你写的代码也没有看出有错误

作者: dream1206   发布时间: 2011-11-10

还是只更新最后一个!

引用 1 楼 dream1206 的回复:
PHP code
foreach( $arr_hang as $lie ){
mysql_query("update `user_list` set ext1 = ext1 + 2500 where `username`='$lie'" );
if( mysql_affected_rows >= 0 )
echo "更新成功:".$lie;
else
echo "……

作者: qqmnme   发布时间: 2011-11-10

这是全部代码、
<?php
$dbhost='localhost';//数据库IP
$dbname = 'sex';//数据库名
$dbuser = 'test';//数据库用户名
$dbpassword = '123';//用户密码
$conn = mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbname,$conn);
mysql_query("set names gb2312");

$content=fread(fopen("pay5.txt",'r+'),filesize("pay5.txt"))."";
$arr_hang=explode("\n",$content);

foreach( $arr_hang as $lie ){
mysql_query("update `user_list` set ext1 = ext1 + 2500 where `username`='$lie'" );
if( mysql_affected_rows >= 0 )
  echo "更新成功:".$lie;
else
  echo "ERROR:".mysql_error();



$of = fopen("pay5.txt",'w+');//创建并打开wml
if($of){
 fwrite($of,$s);//把执行文件的结果写入txt文件
}

?>




引用 1 楼 dream1206 的回复:
PHP code
foreach( $arr_hang as $lie ){
mysql_query("update `user_list` set ext1 = ext1 + 2500 where `username`='$lie'" );
if( mysql_affected_rows >= 0 )
echo "更新成功:".$lie;
else
echo "……

作者: qqmnme   发布时间: 2011-11-10

我写错了一点 mysql_affected_rows() 是个方法
 你把 $arr_hang 打印出来,和数据库中的数据比对一下就知道为啥了

作者: dream1206   发布时间: 2011-11-10

我不新手啊,麻烦您帮忙写个好吗?
引用 4 楼 dream1206 的回复:
我写错了一点 mysql_affected_rows() 是个方法
你把 $arr_hang 打印出来,和数据库中的数据比对一下就知道为啥了

作者: qqmnme   发布时间: 2011-11-10

PHP code

<?php
$dbhost='localhost';//数据库IP
$dbname = 'sex';//数据库名
$dbuser = 'test';//数据库用户名
$dbpassword = '123';//用户密码
$conn = mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbname,$conn);
mysql_query("set names gb2312");
// 读取文件
$readfile = fopen("pay5.txt",'r+');
$content=fread(fopen("pay5.txt",'r+'),filesize("pay5.txt"))."";
fclose( $readfile);
// 分行
$arr_hang=explode("\n",$content);

print_r( $arr_hang);
echo "<br/>";

foreach( $arr_hang as $lie ){
mysql_query("update `user_list` set ext1 = ext1 + 2500 where `username`='$lie'" );
if( mysql_affected_rows() >= 0 )
  echo "更新成功:".$lie;
else
  echo "ERROR:".mysql_error();
}  

// 写入
$of = fopen("pay5.txt",'w+');//创建并打开wml
if($of){
 fwrite($of,$s);//把执行文件的结果写入txt文件    $s未定义吧!
}
?>


每次更新不管有没有成功都会打印出信息的
现在想想也许有乱码的可能。

作者: dream1206   发布时间: 2011-11-10

好的,我试试。非常感谢您!
引用 6 楼 dream1206 的回复:
PHP code

<?php
$dbhost='localhost';//数据库IP
$dbname = 'sex';//数据库名
$dbuser = 'test';//数据库用户名
$dbpassword = '123';//用户密码
$conn = mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbn……

作者: qqmnme   发布时间: 2011-11-10

热门下载

更多