PHP / MySQL访谈 - 你会如何回答?

时间:2012-12-06 16:51:42

标签: php mysql

我被问到这个面试问题,所以我想在此发布,看看其他用户会如何回答:

Please write some code which connects to a MySQL database (any host/user/pass), retrieves the current date & time from the database, compares it to the current date & time on the local server (i.e. where the application is running), and reports on the difference. The reporting aspect should be a simple HTML page, so that in theory this script can be put on a web server, set to point to a particular database server, and it would tell us whether the two servers’ times are in sync (or close to being in sync).

这就是我所说的:

// Connect to database server
$dbhost = 'localhost';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

// Select database
mysql_select_db($dbname) or die(mysql_error());

// Retrieve the current time from the database server
$sql = 'SELECT NOW() AS db_server_time';

// Execute the query
$result = mysql_query($sql) or die(mysql_error());

// Since query has now completed, get the time of the web server
$php_server_time = date("Y-m-d h:m:s");

// Store query results in an array
$row = mysql_fetch_array($result);

// Retrieve time result from the array
$db_server_time = $row['db_server_time'];

echo $db_server_time . '<br />';
echo $php_server_time;

if ($php_server_time != $db_server_time) {
    // Server times are not identical

    echo '<p>Database server and web server are not in sync!</p>';

    // Convert the time stamps into seconds since 01/01/1970
    $php_seconds = strtotime($php_server_time);
    $sql_seconds = strtotime($db_server_time);

    // Subtract smaller number from biggest number to avoid getting a negative result
    if ($php_seconds > $sql_seconds) {
        $time_difference = $php_seconds - $sql_seconds;
    }
    else {
        $time_difference = $sql_seconds - $php_seconds;
    } 

    // convert the time difference in seconds to a formatted string displaying hours, minutes and seconds
    $nice_time_difference = gmdate("H:i:s", $time_difference);

    echo '<p>Time difference between the servers is ' . $nice_time_difference;
}
else {
    // Timestamps are exactly the same
    echo '<p>Database server and web server are in sync with each other!</p>';
}

是的,我知道我已经使用了已弃用的mysql_ *函数,但除此之外,您将如何回答,即您将做出哪些更改以及为什么?我有什么因素可以忽略吗?

有趣的是,在我的托管帐户上执行时,我的结果似乎总是相隔几分钟:

2012-12-06 11:47:07

2012-12-06 11:12:07

4 个答案:

答案 0 :(得分:3)

这将是我的大部分代码:

$db = new PDO(...);
$dbTime = new DateTime(current($db->query('SELECT NOW()')->fetchAll(PDO::FETCH_COLUMN, 0)));
$myTime = new DateTime();
$diff = $myTime->diff($dbTime);
// do stuff with $diff

答案 1 :(得分:2)

$php_server_time = date("Y-m-d h:m:s");

将时间格式化为年 - 月 - 日小时:月:秒。这解释了服务器时间似乎为11:12:07的事实。它实际上说它是12月。数据库时间和服务器时间恰好相差35分钟,非常令人惊讶。即使没有“确切”这个词,也会令人惊讶。

格式字符串中

分钟为i


除此之外,不同的时间不一定意味着数据库与服务器完全同步。它可能只意味着测量之间经过了一些(任意小的)时间。如果要验证同步,可以在服务器上进行两次测量,一次在查询之前,一次在查询之后,并进行范围比较,或者只是delta-比较时间(abs(t1-t2)&lt; = 1s )

答案 2 :(得分:1)

你动手使用已弃用的mysql_函数,但如果我在求职面试中问这个问题,那就是红旗#1。它告诉我,要么你没有跟上PHP目前的最佳实践,要么你不关心。

我要关注的另一件事就是你没有指定MySQL给你时间的格式。我想明确指定输出格式,这样你就不会依赖于任何服务器设置。

答案 3 :(得分:0)

会使用PDO和查询:

SELECT UNIX_TIMESTAMP()

代码:

$time_difference = abs($PDOStatement->fetchColumn() - date('U'));

当差异为<= 1

时,可能会宣布时间“大致相同”