[英]php/mysql: get data from one table, convert and insert in another table
好的,我會試着解釋一下,(我是葡萄牙人,哈哈)。
我有一個數據庫,其中有空閑的 MySQL:
數據庫:廣告
表:筆記本
字段:ID、標簽、so、lastlogon、lastlogh
我的 ldap 查詢從 ldap 服務器(活動目錄)獲取數據並將其存儲在我的 MySQL 數據庫(廣告/筆記本)中。 我得到標簽,標簽編號是唯一的。 我明白這是筆記本中安裝的操作系統。 我得到 lastlogh,它是最后一個登錄時間戳,它也是獨一無二的。 ID 字段是自動增量和鍵,但我可以將標記字段設置為鍵。
我有一個 config_notebooks.php,我在其中設置了所有變量以連接到 ldap 和 MySQL 服務器。
<?php
/*------------------------------------------------------------------------*/
//setting your variables
/*------------------------------------------------------------------------*/
//ldap host
$host = "ldap://HEICPT1VIA01.HEIWAY.NET";
//ldap user
$user = "domain\user";
//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";
?>
現在查詢腳本
<?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")
?>
在此查詢中,最后一次登錄時間戳編碼為 130276262860634000
所以我可以將所有內容導出到 excel 並對其進行解碼,但我找到了一個可以做到這一點的代碼,因此可以節省時間。 我在數據庫 (lastlogon) 中創建了一個新字段,我需要從 lastlogh 字段中獲取數據,使用新腳本進行解碼並將其存儲在 lastlogon 字段中。
這是我找到的腳本:
<?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);
?>
$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]'");
}
我希望這能解決你的問題。
注意: Mysql API 從 PHP 5.5.0 開始被棄用。 因此,強烈建議使用 Mysqli API 進行更新的開發。
男人.. 使用不同的來解決你的問題http://www.w3schools.com/sql/sql_distinct.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.