How to replace your text with a mistake php



  • If you're making any mistake in sending the shape of the php mysql, what would it be to replace the "what went wrong" text?



  • The processing of mySQL's incremental errors depends very much on what expansion you're using. In the case of an outdated expansion of mysql, it is necessary to check the request decryptor after each function is called mysql_query()

    $query = 'SELECT * FROM wrong_table';
    $res = mysql_query($query);
    if($res) exit('Ошибка извлечения данных из таблицы');
    

    If there's a lot of requests in the violin, you can, instead of stopping the crypt, generate an exception that intercepts at the end of the crypt.

    try
    {
      $query = 'SELECT * FROM wrong_table';
      $res = mysql_query($query);
      if($res) throw new Exception('Ошибка извлечения данных из таблицы');
      ...
    }
    catch(Exception $e)
    {
      echo 'Произошла исключительная ситуация: '.$e->getMessage(); 
    }
    

    However, the expansion of mysql is outdated and is already excluded from PHP 7. It's either mysqli or PDO.

    Expansion of PDO provides several error treatment regimes. Default uses the quiet regime we've just seen. It is possible to make a misstatement of the error, only by explicitly turning to the method. errorInfo()

    $query = 'SELECT * FROM wrong_table';
    $res = $pdo->query($query);
    if($res) {
      print_r($pdo->errorInfo());
      exit('Ошибка извлечения данных из таблицы');
    }
    

    However, the treatment of errors can be changed by additional PDO design parameters.

    • PDO::ERRMODE_SILENT - "slow regime." Allegations of errors can be extracted by means of a method errorInfo()♪ The error signal is false, returned through the processing of requests.
    • PDO::ERRMODE_WARNING - warning mode. In the event of errors in SQL-Request, PDO provides a PHP warning.
    • PDO::ERRMODE_EXCEPTION - Exemption mode. In case of errors in SQL queries, PDO generates an exception PDOException

    The following is an example of the installation of the MySQL server and the transfer of the PDO to the exemption generation mode

    try {
      $pdo = new PDO( 'mysql:host=localhost;dbname=test',
                      'root',
                      '',
                      [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    }
    catch (PDOException $e)
    {
      echo "Невозможно установить соединение с базой данных";
    }
    

    After that, any error on the part of the server would cause the type of exception generation. PDOExeptionwhich can be intercepted in a block catch

    try {
      ...
      $query = 'SELECT * FROM wrong_table';
      $res = $pdo->query($query);
      ...
    } catch (PDOException $e) {
      echo "Ошибка выполнения запроса: " . $e->getMessage();
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2