<?php
namespace demo;
use \PDO, \PDOStatement;
function insert(PDOStatement $sth, $sessionId, $customerId, $time = null)
{
if ($time === null) {
$time = date('Y-m-d H:i:s');
}
$sth->execute(array(
':sessionId' => $sessionId,
':customerId' => $customerId,
':time' => $time
));
printf("\tINSERT ('%s', '%s', '%s') --> %s\n",
$sessionId, $customerId, $time, $sth->rowCount());
}
$pdo = new PDO('mysql:dbname=test;host=localhost', 'user', 'pass',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$pdo->query("DROP TABLE IF EXISTS `visitors`");
$pdo->query("
CREATE TABLE IF NOT EXISTS `visitors` (
`visitor_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`session_id` VARCHAR( 255 ) NOT NULL ,
`customer_id` INT( 11 ) NOT NULL ,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`lastchange` DATETIME NOT NULL ,
UNIQUE (
`session_id`
)
) ENGINE = InnoDB
");
$insertStatement = $pdo->prepare("
INSERT INTO `test`.`visitors`
(
`visitor_id` ,
`session_id` ,
`customer_id` ,
`created` ,
`lastchange`
)
VALUES
(
NULL ,
:sessionId ,
:customerId ,
CURRENT_TIMESTAMP ,
:time
)
ON DUPLICATE KEY UPDATE
`customer_id` = :customerId,
`lastchange` = :time
");
header('Content-Type: text/plain; charset=UTF-8');
echo "Insert two rows\n";
insert($insertStatement, 'session1', 1);
insert($insertStatement, 'session1', 1, 'X');
exit;
echo "Update second row in same second\n";
insert($insertStatement, 'session2', 1);
echo "Update second row in same second with new alue\n";
insert($insertStatement, 'session2', 2);
sleep(2);
echo "Sleeping...\n";
#insert($insertStatement, 'session2', 1);
sleep(2);
echo "Sleeping...\n";
#insert($insertStatement, 'session2', 1, 'X');