Ok I will try to explain, (I am Portuguese lol).
I have a Database where I have the fallowing MySQL:
Database: ad
Table: notebooks
Fields: ID, tag, so, lastlogon, lastlogh
My ldap query gets data from ldap server (active directory) and stores it in my MySQL database (ad/notebooks). I get tag which is tag number is unique. I get so which is the OS installed in the notebook. I get lastlogh which is the last logon time stamp, and it is unique too. ID field is auto increment and key but I can set the tag filed to be key.
I have a config_notebooks.php where I set all the variables to connect to ldap and MySQL servers.
<?php /*------------------------------------------------------------------------*/ //setting your variables /*------------------------------------------------------------------------*/ //ldap host $host = "ldap://HEICPT1VIA01.HEIWAY.NET"; //ldap user $user = "domainuser"; //ldap password $pswd = "pssw"; //ldap base structure $dn = "OU=NotebookM2,OU=WorkstationsM2,OU=PT1,DC=heiway,DC=net";; //attributs to search and get $attrs = array("cn","operatingsystem","lastlogon"); //sql host $sqlhost="localhost"; //sql user $sqluser="root"; //sql password $sqlpswd=""; //sql database $database="ad"; //sql table $table="notebooks"; ?>
Now the query script
<?php /*------------------------------------------------------------------------*/ //Query script /*------------------------------------------------------------------------*/ include 'config_notebooks.php'; $filter = $_POST['filter']."=".$_POST['keyword']."*"; //connect to db $con = mysql_connect("$sqlhost","$sqluser",""); if (!$con) { die('Could not connect: ' . mysql_error()); } //connect to active directory $ad = ldap_connect($host) or die( "Could not connect!" ); // Set version number ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) or die ("Could not set ldap protocol"); // Binding to ldap server $bd = ldap_bind($ad, $user, $pswd) or die ("Could not bind"); $search = ldap_search($ad, $dn, $filter, $attrs) or die ("ldap search failed"); $entries = ldap_get_entries($ad, $search); $sel = mysql_select_db("ad", $con); if (!$sel) { die('Could not select DB: ' . mysql_error()); } for ($i=0; $i<$entries["count"]; $i++) { $tag = $entries[$i]["cn"][0]; $so = $entries[$i]["operatingsystem"][0]; $lastl = $entries[$i]["lastlogon"][0]; mysql_query(" INSERT INTO $table (tag, so, lastlogh) VALUES ('$tag', '$so', '$lastl') ON DUPLICATE KEY UPDATE tag='$tag', so='$so', lastlogon='$lastl' "); } mysql_close($con); ldap_unbind($ad); if ($entries["count"] > 0) header("Location: notebooks_list.php") ?>
In this query the last logon timestamp is coded like 130276262860634000
So I can export everything to excel and decode it, but I found a code that does that, so it saves time. I created a new field in the database (lastlogon) and I need to fetch the data from lastlogh field, decode with the new script and store it in the lastlogon field.
This is the script that I found:
<?php function adConvert ($ad) { $seconds_ad = $ad / (10000000); //86400 -- seconds in 1 day $unix = ((1970-1601) * 365 - 3 + round((1970-1601)/4) ) * 86400; $timestamp = $seconds_ad - $unix; $normalDate = date("F d, Y", $timestamp); return $normalDate; } //example: echo adConvert($ad); ?>
Advertisement
Answer
Using Mysql API
$res = mysql_query("SELECT * FROM $table"); while($row = mysql_fetch_assoc($res)) { $logon_ts = adConvert($row['lastlogh']); mysql_query("UPDATE $table SET lastlogon = '$logon_ts' WHERE tag='$row[tag]'"); }
I hope this solves your problem.
Note: Mysql API is deprecated as of PHP 5.5.0. So, It’s highly recommended to use Mysqli API for newer developments.