Page 1 of 1

Use of pdo query

Posted: Sun Jun 23, 2024 11:03 pm
by mpol_ch

Hi
I get the following code that use pdo connection code.
I want to call "function getHolidayFTag($date, $kanton, $pdo)" from Row_Rendered or Row_Inserted server event.
In this script is database, username etc is given.
How can I integrate this script into phpmaker 2024 to call the defined functions in it?

  // Database connection details
  $dsn = 'mysql:host=localhost;dbname=ttracker;charset=utf8';
  $username = 'Gers***';
  $password = '*********';
  $options = [
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES => false,
  ];

  try {
      // Establish PDO connection
      $pdo = new PDO($dsn, $username, $password, $options);
  } catch (PDOException $e) {
      // Handle connection error
      die("Error: " . $e->getMessage());
  }

  // Function to check if a date is a holiday in a specific Kanton and get FTag
  function getHolidayFTag($date, $kanton, $pdo) {
      $stmt = $pdo->prepare("SELECT FTag FROM feiertage WHERE Datum = ? AND Nummer = ?");
      $stmt->execute([$date, $kanton]);
      $result = $stmt->fetch();
      return $result ? $result['FTag'] : 0;
        }
      ........

mpol_ch


Re: Use of pdo query

Posted: Mon Jun 24, 2024 8:52 am
by mobhar

As long as you are using the main database in your project, or even from the Linked Tables, then there is no need to create another database connection in order to use your global function.

Instead, you may put your code in Global Code, and you may use ExecuteScalar global function to get a single value from your database.