Posted By

konfeldt on 06/19/13


Tagged

mysql php user update record


Versions (?)

Update_User_Record


 / Published in: PHP
 

URL: http://konfeldt.com

PHP/MySQL script that update a user record

  1. <?php
  2. // Get the passed argument
  3. $pass = $_GET['pass'];
  4.  
  5. // Check if the passed password matches my own
  6. if (isset($pass)) {
  7.   if ($pass !== 'some_password_you_choose') {
  8.     die('You can\'t use this function! Contact web admin for more info!');
  9.   }
  10. } else {
  11.   die('You can\'t use this function! Contact web admin for more info!');
  12. }
  13.  
  14. // Connect to MySQL<br />
  15. $con = mysql_connect("localhost","user_name","password");
  16. if (!$con)
  17. {
  18.   die('Could not connect: ' . mysql_error());
  19. }
  20.  
  21. // Select the DB to work with
  22. mysql_select_db("database_name", $con);
  23.  
  24. // Get the number of users
  25. $result = mysql_query("SELECT * FROM table_name", $con);
  26. $num_rows = mysql_num_rows($result);
  27.  
  28. // Generate unique hash
  29. function genRandomString($len) {
  30.   $shuffled = str_shuffle('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
  31.   return substr($shuffled, 0, $len);
  32. }
  33.  
  34. // Call the hash generating function and update MySQL
  35. for ($i = 1; $i <= $num_rows; $i++) {
  36.   $uniquehash = genRandomString(32);
  37.   mysql_query("UPDATE table_name SET uniqueidstring = '$uniquehash' WHERE id = '$i'") or die(mysql_error());
  38.   $uniquehashlenght = strlen(utf8_decode($uniquehash));
  39.   echo "Updated record <b>$i</b> out of total of $num_rows records with Unique Hash {$uniquehashlenght}: <b>$uniquehash</b>.<br />\n";
  40. }
  41.  
  42. // Close MySQL Connection
  43. ?>

Report this snippet  

You need to login to post a comment.