Handle exceptions.

This commit is contained in:
Igor Barkov (iwork) 2020-06-23 13:49:12 +03:00
parent 1b6cf440c2
commit 0892729efb

View File

@ -14,26 +14,36 @@ include_once "config.php";
$msg = filter_input(INPUT_POST,'msg'); $msg = filter_input(INPUT_POST,'msg');
$uid = $_SESSION['uid']; $uid = $_SESSION['uid'];
if ($msg) db::c()->query('INSERT INTO `chat` (`uid`, `msg`) VALUES (?i, "?s")', $uid, $msg); if ($msg) {
try {
db::c()->query('INSERT INTO `chat` (`uid`, `msg`) VALUES (?i, "?s")', $uid, $msg);
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "DB Error:". $e->getMessage() ."On Line:". $e->getLine();
}
}
function show_messages() function show_messages()
{ {
$chat = db::c()->query(' try {
SELECT $chat = db::c()->query('
`msg`, SELECT
`msgdate`, `msg`,
(SELECT `login` FROM `users` WHERE `users`.`id` = `uid`) AS `from`, `msgdate`,
`type` (SELECT `login` FROM `users` WHERE `users`.`id` = `uid`) AS `from`,
FROM `chat` ORDER BY `id` ASC LIMIT 50'); `type`
FROM `chat` ORDER BY `id` LIMIT 50');
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "DB Error:". $e->getMessage() ."On Line:". $e->getLine();
}
while ($message = $chat->fetch_assoc()) { while ($message = $chat->fetch_assoc()) {
$d = new DateTime($message['msgdate']); $d = new DateTime($message['msgdate']);
$m = htmlspecialchars($message['msg']); $m = htmlspecialchars($message['msg']);
if ($message['type'] == 'sys') { /* Системка */ if ($message['type'] == 'sys') { /* Системка */
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span>', $d->format('H:i'),$m)." <br>"; echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span>', $d->format('H:i'),$m).PHP_EOL;
} elseif ($message['type'] == 'sms') { /* Телеграмма */ } elseif ($message['type'] == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span>', $d->format('H:i'),$m)." <br>"; echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span>', $d->format('H:i'),$m).PHP_EOL;
} else echo sprintf('%s [%s]: %s', $d->format('H:i'),$message['from'],$m)." <br>"; } else {echo sprintf('%s [%s]: %s', $d->format('H:i'),$message['from'],$m).PHP_EOL;}
} }
} }
show_messages(); show_messages();