当前位置:Gxlcms > PHP教程 > php批量删除mySQL数据库记录

php批量删除mySQL数据库记录

时间:2021-07-01 10:21:17 帮助过:30人阅读

  1. #Usage: php mysql_rmByKey.php
  2. #Usage: php mysql_rmByKey.php
  3. #count($argv) > 0, the first $argv[0] is the php file
  4. if(count($argv) == 7){
  5. $user = $argv[1];
  6. $passwd = $argv[2];
  7. $db = $argv[3];
  8. $table = $argv[4];
  9. $key = $argv[5];
  10. $delFile = $argv[6];
  11. }else if(count($argv) == 5){
  12. $user = 'user';
  13. $passwd = '123456';
  14. $db = $argv[1];
  15. $table = $argv[2];
  16. $key = $argv[3];
  17. $delFile = $argv[4];
  18. }else{
  19. #usage();
  20. echo '
  21. Usage: php mysql_rmByKey.php '."\n";
  22. }
  23. $link = mysql_connect('localhost',$user,$passwd);
  24. if(!$link){
  25. die("Could not connect to mysql server: ". mysql_error());
  26. }
  27. mysql_select_db($db,$link);
  28. $fp = fopen($delFile, 'r');
  29. while(!feof($fp) && $ln = fgets($fp)){
  30. $id = chop($ln);
  31. $sql = "delete from $table where $key = '$id';";
  32. mysql_query($sql, $link);
  33. }
  34. fclose($fp);
  35. mysql_close($link);
  36. ?>

人气教程排行