This commit is contained in:
2026-01-23 23:45:16 +01:00
parent 0a784ef454
commit ed5d77bf6c
4 changed files with 23 additions and 10 deletions

View File

@@ -1,13 +1,7 @@
<?php
// public/api/_db.php
$cfg = require __DIR__ . '/../../src/config.php';
$dsn = "mysql:host={$cfg['db_host']};dbname={$cfg['db_name']};charset={$cfg['db_charset']}";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
try {
$pdo = new PDO($dsn, $cfg['db_user'], $cfg['db_pass'], $options);
require_once __DIR__ . '/../../tools/db.php';
$pdo = tools_build_pdo();
} catch (PDOException $e) {
http_response_code(500);
header('Content-Type: application/json');

View File

@@ -2,5 +2,6 @@
// public/api/printers.php
header('Content-Type: application/json; charset=utf-8');
require __DIR__ . '/_db.php';
$stmt = $pdo->query("SELECT * FROM printers WHERE is_active = 1 ORDER BY name");
echo json_encode($stmt->fetchAll(), JSON_UNESCAPED_UNICODE);
require_once __DIR__ . '/../../tools/printers.php';
$printers = tools_fetch_active_printers($pdo);
echo json_encode($printers, JSON_UNESCAPED_UNICODE);

12
tools/db.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
function tools_build_pdo(): PDO
{
$cfg = require __DIR__ . '/../src/config.php';
$dsn = "mysql:host={$cfg['db_host']};dbname={$cfg['db_name']};charset={$cfg['db_charset']}";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
return new PDO($dsn, $cfg['db_user'], $cfg['db_pass'], $options);
}

6
tools/printers.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
function tools_fetch_active_printers(PDO $pdo): array
{
$stmt = $pdo->query("SELECT * FROM printers WHERE is_active = 1 ORDER BY name");
return $stmt->fetchAll();
}