Php 在函数中插入日期检查
我有一个函数,它从一个名为“customer”的表中加载一个数组。它工作正常,但我还希望它检查客户是否已过期,如果已过期,则显示一个错误 因此,我有下面的代码来检查id\u到期日期是否已过,但我很难确定将其放在函数中的什么位置。试过几次,但似乎都失败了。请帮忙Php 在函数中插入日期检查,php,postgresql,function,Php,Postgresql,Function,我有一个函数,它从一个名为“customer”的表中加载一个数组。它工作正常,但我还希望它检查客户是否已过期,如果已过期,则显示一个错误 因此,我有下面的代码来检查id\u到期日期是否已过,但我很难确定将其放在函数中的什么位置。试过几次,但似乎都失败了。请帮忙 function customer_load_by_id($id) { $dbh = dbh_get(); $id = array(); $sql = 'select c.* ' . 'from custom
function customer_load_by_id($id)
{
$dbh = dbh_get();
$id = array();
$sql = 'select c.* ' .
'from customer c ' .
'where c.cust_id = (select max(cust_id) from customer where id = ?)';
$stmt = $dbh->prepare($sql);
$stmt->execute(array($id));
$r = $stmt->fetch();
if (!is_bool($r)) {
$fields = id_get_fields();
foreach ($fields as $field) {
$n = $field['name'];
$id[$n] = $r[$n];
}
}
dbh_free($dbh);
$exp_date = "$id_expiry_date";
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
if ($expiration_date > $today) {
//continue happily
} else
{
//don't continue
}
return $id;
}
// One of the fields in the array above is id_expiry_date
我并不真正了解SQL模式,但是,使用Pomm,我将编写以下代码:
<?php
use PommProject\Foundation\Pomm;
$loader = require __DIR__ . '/vendor/autoload.php'; //<- setup autoloading
$pomm = new Pomm(…); // <- connection params go there
$sql = <<<"SQL"
select
c.*,
c.expiry_date < now() as is_expired
from customer c
where c.cust_id = (select max(cust_id) from customer where id = $*)
SQL;
$result = $pomm
->getDefaultSession()
->getQueryManager()
->query($sql, [$id])
->current() // <- Take the first row if exist
;
if ($result) {
if ($result['is_expired']) { //<- 'is_expired' is converted as boolean
echo "expired";
} else {
echo "not expired";
}
} else {
echo "no result";
}
把它放在胸径和免费($dbh)之间
和返回$id
然后返回一个关于它为什么会中断的问题。确定编辑了代码,为什么会中断?看起来$id\u expiration\u date未初始化。您是否收到任何错误消息?这将是一个单独的函数?它不处理加载数组