我通过ODBC连接连接到云数据库:
$conn = odbc_connect('MYDATABASE','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql = "SELECT DATETIME_ID, NAME, Sum(CNDROP) AS DATA
FROM MY_TABLE
WHERE DATETIME_ID>='2014-09-28:00:00:00'
and DATETIME_ID<='2014-09-28 23:00:00'
and NAME IN ('CC2')
GROUP BY DATETIME_ID, NAME ORDER BY DATETIME_ID, NAME";
$rs = odbc_exec($conn,$sql);
if (!$rs) {
exit("Consulta fallida");
}
$result = odbc_exec($conn,$sql) or die(exit("Error en odbc_exec"));
print odbc_result_all($result,"border=1");
odbc_close($conn);
我可以获取数据并打印数据,但现在我需要将数据插入到我的计算机的MySQL数据库中。
我不知道该怎么做,所以我需要一个例子的帮助。我试图在谷歌搜索,但没有任何帮助。
答案 0 :(得分:1)
选择功能
function get_data_from_cloud(){
$conn=odbc_connect('CLOUD','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql="SELECT DATETIME, NAME, CNDROP
FROM TABLE1
WHERE DATETIME>='2014-09-28 00:00:00' and
DATETIME<='2014-09-28 23:00:00' and
NAME IN ('PETER')
GROUP BY DATETIME, NAME
ORDER BY DATETIME, NAME";
$result=odbc_exec($conn,$sql)or die(exit("Error en odbc_exec"));
$data = array();
while (odbc_fetch_row($result)) {
$data[]=array('DATETIME' => odbc_result ($result, "DATETIME"),
'NAME'=> odbc_result ($result, "NAME"),
'CNDROP'=> odbc_result ($result, "CNDROP"));
}
return $data;
}
INSERT功能
function insert_cloud_data($cloud_data=array()){
$conn=odbc_connect('LOCAL','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
foreach($cloud_data as $data){
$sql = sprintf("INSERT INTO Prueba (DATIME, NAME, CNDROP)
VALUES ( '%s','%s','%s')",
$data['DATETIME'], $data['NAME'], $data['CNDROP']);
$rs = odbc_exec($conn,$sql);
if (!$rs) {
error_log("Consulta fallida");
}
}
odbc_close($conn);
}
<小时/>
选择功能
function get_data_from_cloud(){
$conn=odbc_connect('CLOUD','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql="SELECT DATETIME, NAME, CNDROP
FROM TABLE1
WHERE DATETIME>='2014-09-28 00:00:00' and
DATETIME<='2014-09-28 23:00:00' and
NAME IN ('PETER')
GROUP BY DATETIME, NAME
ORDER BY DATETIME, NAME";
$result=odbc_exec($conn,$sql)or die(exit("Error en odbc_exec"));
$data = array();
while (odbc_fetch_row($result)) {
$data[]=array(odbc_result ($result, "DATETIME"),
odbc_result ($result, "NAME"),
odbc_result ($result, "CNDROP"));
}
return $data;
}
INSERT功能
function insert_cloud_data($cloud_data=array()){
$conn=odbc_connect('LOCAL','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$sql = "INSERT INTO Prueba (DATIME, NAME, CNDROP)
VALUES (?, ?, ?)";
$stmt = odbc_prepare($conn, $sql);
if(!$stmt) die("could not prepare statement ".$sql);
foreach($cloud_data as $data){
odbc_execute($stmt, $data);
}
odbc_close($conn);
}
<强> USAGE 强>
$cloud_data = get_data_from_cloud();
insert_cloud_data($cloud_data);
答案 1 :(得分:0)
这是不同的方法。
创建$ conn1(云)和$ conn2(localhost)。查询$ conn1然后使用php while和insert命令到$ conn2。
$conn1 = (cloud);
$conn2 = (localhost);
$query = "SELECT ...";
$result = odbc_exec($conn1,$query);
while( fetch result data ) {
$query = "INSERT ....";
odbc_exec($conn2,$query);
}