function.php 824 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<?php

/**
 * get float microtime
 * @return float
 */
function microtimeFloat() {
  list($usec, $sec) = explode(' ', microtime());
  return ((float) $usec + (float) $sec);
}

/**
 * show message
 * @param string $str
 * @param bool $die
 */
function message($str, $die = false) {
  $now = date('Y-m-d H:i:s');
  print "[{$now}] {$str}\n";

  if($die) {
    exit(1);
  }
}

/**
 * select data from db
 * @param \PDO $pdo
 * @param string $query
 * @param array $params
 * @param string $error
 * @return array
 */
function selectDb(\PDO $pdo, $query, array $params = [], &$error = '') {
  $stmt = $pdo->prepare($query);

  if(!$stmt) {
    $error = $pdo->errorInfo()[2];
    return [];
  }

  if(!$stmt->execute($params)) {
    $error = $pdo->errorInfo()[2];
    return [];
  }

  return $stmt->fetchAll(\PDO::FETCH_OBJ);
}