Converting MySQL rows/fields
Added by astuffedtiger on May 16, 2017 19:22
<?php

/* This script could be used as a template when you need to convert a field value into something else. I used it when converting from MySQL DATATIME to Unix time as well as hashing out new passwords from old ones. */

include('config.php');
include('includes/password.php');
/* Make a connection to the database. */
$db = new mysqli("$dbhost", "$dbuser", "$dbpassword", "$dbname");
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

echo "<pre>";

$result = mysqli_query($db, "SELECT * FROM users");
if (!$result) {
    echo 'Could not run query: ' . ((is_object($db)) ? mysqli_error($db) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false));
}
while ($row = mysqli_fetch_array($result)) {
    $userid = $row['userid'];
    $password = $row['password'];
    
    $hash = password_hash($password, PASSWORD_DEFAULT);
    
    echo "Hashing Password to new format.........<br />UserID: $userid.........<br /> Current Hash: $password..........<br />New Hash:$hash......<br /><br />";
    
    mysqli_query($db, "UPDATE users SET password2 = '$hash' WHERE userid = '$userid'");
	
}

echo "</pre>";
?>

Comments