1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #!/usr/local/bin/php
<?php
ini_set('display_errors', FALSE);
define('DSN', 'mysql:host=localhost;dbname=developpez');
define('LOGIN', 'developpez');
define('MOT_DE_PASSE', 'developpez');
define('LOG', '/var/log/rewritemap.log');
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
try {
$dbh = new PDO(DSN, LOGIN, MOT_DE_PASSE, $options);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$map = $dbh->prepare('SELECT id FROM articles WHERE nom = ?');
$map->bindParam(1, $input, PDO::PARAM_STR);
$map->bindColumn(1, $output, PDO::PARAM_INT);
set_file_buffer(STDOUT, 0);
while (!feof(STDIN)) {
$input = rtrim(fgets(STDIN), "\n");
$map->execute();
if ($map->rowCount() === 1) {
$map->fetch();
fputs(STDOUT, "$output\n");
} else {
fputs(STDOUT, "NULL\n"); // S'il n'y a pas de correspondance, Apache attend 'NULL'
}
}
} catch (Exception $e) {
file_put_contents(LOG, sprintf('[%s:%d] %s: %s' . PHP_EOL, $e->getFile(), $e->getLine(), date('d/m/Y H:i:s'), $e->getMessage()), FILE_APPEND);
} |
Partager