Z
Z
Zrivoknovsky2021-06-01 21:30:38
css
Zrivoknovsky, 2021-06-01 21:30:38

How to add exception to php script?

Hello. I can't figure it out, I need help.
There is a php script that takes information about printers from a sql table and queries them via snmp. You need to add exceptions so that part of the code with checks and calculations is not executed if the printer is not available (turned off for example).
As I understand it, you need to use try catch for this, I have already reviewed a bunch of articles and forums, but I just can’t implement it.
The script itself

<?php
include("db-config.php");// Файл конфигупации подключения к БД

//Запрос данных из таблицы принтеров
$Printer = mysqli_query($link, "SELECT p_id,p_ip,p_snmp,p_counter,p_cartridge_id  FROM Printer WHERE p_status=1");

while($ms_res1 = mysqli_fetch_assoc($Printer))
{
  $p_id = $ms_res1['p_id'];//Идентификатор
  $p_ip = $ms_res1['p_ip'];//ip адрес
  $p_snmp = $ms_res1['p_snmp'];//snmp код
  $p_counter = $ms_res1['p_counter'];//Счетчик напечатаных страниц из БД
  $p_cartridge_id = $ms_res1['p_cartridge_id'];//Идентификатор картриджа
  $community='public';//
  snmp_set_quick_print (1);//1-Только значение, 0-Полное отображение
*****На этом этапе хочу проверить есть ли связь с принтером 
*****и остальную часть выполнять только если он доступен
  $printer_snmp_counter=(int)snmpget($p_ip,$community,$p_snmp);//snmp запрос текущего кол-ва напечатаных страниц

//Запрос данных из таблицы картриджей
  $Cartridge = mysqli_query($link, "SELECT c_alarm,c_counter FROM Cartridge WHERE c_id=$p_cartridge_id");

    $ms_res2 = mysqli_fetch_assoc($Cartridge);
    $c_alarm = $ms_res2["c_alarm"];//Остаток ресурса до уведомления
    $c_counter = $ms_res2["c_counter"];//Ресурс картриджа

  $limit = $c_counter - $c_alarm;//Расчет лимита печати
  $tm = ($printer_snmp_counter - $p_counter);//Расчет количеста напечатаных страниц
  echo "Test"."\n";

  if($tm > $limit):
#    send alarm
    $sql = "UPDATE Printer SET p_snmp_counter=$printer_snmp_counter WHERE p_id=$p_id";//Запись в БД
    mysqli_query($link, $sql);
  else:
    $sql = "UPDATE Printer SET p_snmp_counter=$printer_snmp_counter WHERE p_id=$p_id";//Запись в БД
    mysqli_query($link, $sql);
  endif;
}
?>

I'm new, this is my first job with php.
Can you tell me how to throw an exception?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2019-02-04
@valya0502

You need to write code that implements the intended animation.
Animations can be implemented using:
- css
- js
- svg
Choose any option, type "animation {{variant}}>" into the search and enjoy the abundance of tutorials, examples, etc.

R
rPman, 2021-06-01
@rPman

Immediately after calling snmpget, check $printer_snmp_counter for false, if so, skip further steps and display an error or whatever you need to do there

D
Daria Motorina, 2021-06-01
@glaphire

You need to catch the exception where the function can throw it, in your case I don't see any functions that can do it (because the code is procedural style with simple functions, not OOP). It is enough to check for incorrect values, as rPman wrote.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question