Home  >  Article  >  Backend Development  >  PHP batch delete mySQL database records

PHP batch delete mySQL database records

WBOY
WBOYOriginal
2016-07-25 09:12:421196browse
  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. ?>
复制代码



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn