功能问题

时间:2014-07-24 02:59:18

标签: php function pdo

我目前正在尝试制作一个使用函数计算页面加载时间的脚本。我真的想保留PDO方面的东西,将数据插入数据库。

的index.php

<?php
require 'common.php';
require 'page_load_statistics.php';

page_load_start();
?>

<!DOCTYPE html>
<html>
<head>
<title> Home</title>
</head>
<body>
</body>
</html>

<?php
//End The Page Load Statistics
page_load_end();
?>

page_load_statistics.php

<?php
 function page_load_start() {
   $time = microtime(true);
 }

 function page_load_end() {
   $query = "INSERT INTO page_statistics ( page_name, page_load_time ) VALUES(:page_name, :page_load_time ) ";        

   $page_name = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

   $page_load_time = (microtime(true) - $time);

   $query_params = array( ':page_name' => $page_name, ':page_load_time' => $page_load_time); 

    try 
        {  
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
    catch(PDOException $ex) 
        {  
        die("Failed to run query"); 
        } 
}
?>

的common.php

    <?php 

    //Start Session Globally
        session_start();

    //PDO Connection & Settings
    $username = "private_info"; 
    $password = "private_info"; 
    $host = "localhost"; 
    $dbname = "private_info"; 

    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

    try 
    { 
        $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
    } 
    catch(PDOException $ex) 
    {
    die("Failed to connect to the database: " . $ex->getMessage()); 
    }

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 

 //UNDO Magic Quotes

    if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
    { 
        function undo_magic_quotes_gpc(&$array) 
        { 
            foreach($array as &$value) 
            { 
                if(is_array($value)) 
                { 
                    undo_magic_quotes_gpc($value); 
                } 
                else 
                { 
                    $value = stripslashes($value); 
                } 
            } 
        } 
            undo_magic_quotes_gpc($_POST); 
            undo_magic_quotes_gpc($_GET); 
            undo_magic_quotes_gpc($_COOKIE); 
        } 

//Define Content Type
    header('Content-Type: text/html; charset=utf-8'); 

?>

1 个答案:

答案 0 :(得分:1)

问题是范围,正如@ Fred-ii-所指出的那样。您的函数无权访问您在其他地方声明的变量,即$time$db

试试这个,并记下我添加的评论:

的index.php:

<?php
  require 'common.php';
  require 'page_load_statistics.php';

  // store in a variable that will be sent to page_load_end
  $start_time = page_load_start(); 
?>

...html...

<?php
  //End The Page Load Statistics

  // Pass the DB object and the start time to this function
  page_load_end($db, $start_time);
?>

page_load_statistics.php:

<?php
 function page_load_start() {
    // return the start time so later you can set the 
    // start time with $start_time = page_load_start()
    return microtime(true); 
 }

 // Send to this function the DB object created in 'common.php', 
 // as well as the start time
 function page_load_end($db, $start_time) {
   $query = "INSERT INTO page_statistics ( page_name, page_load_time ) VALUES(:page_name, :page_load_time ) ";        

   $page_name = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

   $page_load_time = (microtime(true) - $start_time);

   $query_params = array( ':page_name' => $page_name, ':page_load_time' => $page_load_time); 

    try 
        {  
            $stmt = $db->prepare($query); 
            $result = $stmt->execute($query_params); 
        } 
    catch(PDOException $ex) 
        {  
        die("Failed to run query"); 
        } 
}
?>