Thank you so much, I thought I would never achieve this.
although I'm a complete noob on php, you can't beat the logic so I followed the logic and modified the code.
I was getting this error:
1 lines found
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'removed'@'localhost' (using password: NO) in /home/removed/public_html/au2GzeEf7Qxbv8YVcn5qFUBX6jJsSDTw/NewFolder/sneakyimp.php on line 18
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/removed/public_html/au2GzeEf7Qxbv8YVcn5qFUBX6jJsSDTw/NewFolder/sneakyimp.php on line 18
Warning: mysql_query() [function.mysql-query]: Access denied for user 'removed'@'localhost' (using password: NO) in /home/removed/public_html/au2GzeEf7Qxbv8YVcn5qFUBX6jJsSDTw/NewFolder/sneakyimp.php on line 19
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/removed/public_html/au2GzeEf7Qxbv8YVcn5qFUBX6jJsSDTw/NewFolder/sneakyimp.php on line 19
query failed:Access denied for user 'removed'@'localhost' (using password: NO)
I figured it couldn't login to my DB because dbname dbpass db was never defined so I modified th code like this:
config.php:
<?php
$host="localhost";
$dbUserName="myDBusername";
$dbPassword="myDBpassword";
$dbName="myDBname";
?>
<?php
include("config.php");
$contents = file_get_contents('my path to /.htpasswd');
$lines = preg_split('/\n/', $contents, -1, PREG_SPLIT_NO_EMPTY);
echo count($lines) . " lines found\n";
$users_seen = array();
foreach($lines as $line) {
$clean_line = trim($line);
if (preg_match('/^(.+):(.+)$/', $clean_line, $matches)) {
$user = trim($matches[1]);
$pass = trim($matches[2]);
$users_seen[] = $user; // remember all the users we've seen
// query to see if record already exists "i added below three lines and was surprised i did it correctly :)"
mysql_connect("localhost",$dbUserName,$dbPassword) or die(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
mysql_query($sql);
$sql = "SELECT * FROM myDBtable WHERE myUserColumn='" . mysql_real_escape_string($user) . "'";
$result = mysql_query($sql)
or die('query failed:' . mysql_error());
if (mysql_num_rows($result) == 0) {
// insert the record
$sql = "INSERT INTO myDBtable (myUserColumn, myPasswordColumn) VALUES ('" . mysql_real_escape_string($user) . "', '" . mysql_real_escape_string($pass) . "')";
mysql_query($sql)
or die('insert query failed:' . mysql_error());
}
} else {
// bad line? just ignore it or error here
}
}
// finally, delete all user records we didn't see in the file
foreach($users_seen as $key => $user) {
$users_seen[$key] = "'" . mysql_real_escape_string($user) . "'"; // escape and quote the usernames
}
$sql = "DELETE FROM myDBtable WHERE myUserColumn NOT IN (" . implode(',', $users_seen) . ")";
msyql_query($sql)
or die('delete query failed');
?>
at this point new users are added to DB but removed users not deleted and i get this error:
2 lines found
Fatal error: Call to undefined function msyql_query() in /home/removed/public_html/au2GzeEf7Qxbv8YVcn5qFUBX6jJsSDTw/NewFolder/sneakyimp.php on line 41