Use of pdo query

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mpol_ch
User
Posts: 884
Location: Switzerland

Use of pdo query

Post 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


mobhar
User
Posts: 11828

Post 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.


Post Reply