-1

再会,

我需要有关以下问题的帮助。我需要从一个表中检索数据并插入到另一个表中。我遇到的问题是这两点:

  1. 脚本需要从上次导入继续,以防出现故障
  2. 脚本还应该检测脚本的另一个实例是否已经在进行中并退出

这是我的代码:

<?php
/*echo "<pre>";
print_r(get_defined_functions());
echo "</pre>";*/

echo "testX";
try {
    $link = mysql_connect('host', 'username', 'password');
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

if (!$link)
  {
  die('Could not connect: ' . mysql_error());
  }
else
{
echo "link worked!";
}
mysql_select_db("aaatrycmpg_db2", $link);
$result = mysql_query("SELECT * FROM  source_cdr");
//print_r($result);
$count = mysql_num_rows($result);
echo "<table border='1'><tr><td>callstart</td><td>src</td><td>dst</td>                  <td>accountcode</td><td>uniqueid</td><td>ID</td><td>callanswer</td><td>callend</td> <td>disposition</td><td>cdr_id</td><td>pin_code</td><td>provider</td></tr>";
 while($row = mysql_fetch_array($result))
  {
echo "<tr><td>".$row['callstart']."</td><td>".$row['src']."</td><td>".$row['dst']."    </td><td>".$row['accountcode']."</td><td>".$row['uniqueid']."</td><td>".$row['ID']."</td><td>".$row['callanswer']."</td><td>".$row['callend']."</td><td>".$row['disposition']."</td><td>".$row['cdr_id']."</td><td>".$row['pin_code']."</td><td>".$row['provider']."</td></tr></table>";
  $callstart = $row['callstart'];
  $callanswer = $row['callanswer'];
  $callend = $row['callend'];
  $datetime=$callstart; 
$date=substr($datetime, 0, 10); 
echo $date."<br>"; 
  $callduration = strtotime($callend) - strtotime($callstart);
  $talkduration = strtotime($callend) - strtotime($callanswer);
  echo strtotime($callend)." - ".strtotime($callstart)." = ".$callduration." = ".$talkduration;
  echo "<br />";
  if ($row['cdr_id'] != '1')
  {
mysql_query("INSERT INTO destination_cdr     (calldate,source,destination,account_code,pincode,duration_call,duration_talk,disposition,clid,cdr_id,provider)
VALUES ('".$callstart."','".$row['src']."','".$row['dst']."','".$row['accountcode']."','".$row['pin_code']."',".$callduration.",".$talkduration.",'".$row['disposition']."','".$row['ID']."','".$row['cdr_id']."','".$row['provider']."')");
  }
  }


?>
4

1 回答 1

1
  1. 您没有正确格式化查询部分。
  2. 你根本不需要 PHP。

做吧

INSERT INTO destination_cdr
(calldate,source,destination,account_code,pincode,duration_call,duration_talk,disposition,clid,cdr_id,provider) 
SELECT callstart,src, etc. FROM source_cdr

日期也可以重新格式化,使用 TO_DAYS() 或类似的 mysql 函数

我根本看不到所有这些移动的混乱有多大用处。为什么不将所有记录保存在 source_cdr 中?

于 2013-02-17T10:13:59.713 回答