2017-06-02 73 views
0

我需要控制谁输入每个子页面,并将这些信息插入到MySQL中。我有MYSQL表,它将得到的信息(用户ID,子页面,子页面,日期时间)。创建记录列表并在稍后插入PHP MYSQL

function logstore(){ 
global $DB, $USER, $CFG; 


$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
=== FALSE ? 'http' : 'https'; 
$host  = $_SERVER['HTTP_HOST']; 
$script = $_SERVER['SCRIPT_NAME']; 

$currentUrl = $protocol . '://' . $host . $script; 

$newsurl = $CFG->wwwroot.'/news/view.php'; 
$produrl = $CFG->wwwroot.'/prod/view.php'; 
$materialurl = $CFG->wwwroot.'/material/index.php'; 


$datetime = date_create()->format('Y-m-d H:i:s'); 


$records = new stdClass(); 
$records->userid = $USER->id; 
if($currentUrl == $newsurl){ 
    $records->activity = 'news'; 
    $records->activityid = $_GET['id']; 
}elseif ($currentUrl == $produrl){ 
    $records->activity = 'prod'; 
    $records->activityid = $_GET['id']; 
} 
elseif ($currentUrl == $materialurl){ 
    $records->activity = 'material'; 
    $records->activityid = $_GET['id']; 
} 

$records->datetime = $datetime; 

$DB->insert_record('logstore', $records);} 

林呼吁/news/view.php,/prod/view.php,/material/index.php顶部此功能。

它的工作正常,但我需要优化一点。有什么方法可以在数据库空闲时插入这些记录,或创建列表并每小时插入它们?

//编辑

我试图延迟插入,但它不是我的DB :(

回答

0

有Moodle里/ PHP没有标准的方式来拖延查询,直到DB是自由的工作。 “INSERT DELAY”在最新的MySQL是depracated,你可以在这里看到:https://dev.mysql.com/doc/refman/5.7/en/insert-delayed.html 我可以建议两种选择:

  1. 检查使用http://mysqltuner.pl你的数据库性能 - 也许更多的内存/缓存可以在性能帮助

  2. 对此日志使用另一台服务器 - 所以负载将分布在更多的服务器上。