2015-07-19 45 views
-1

我有这个功能加载从一个名为“客户”正常工作表中的数组,但我也希望它来检查,如果客户已经过期,如果是显示错误插入日期检查到功能

所以我有下面的代码来检查id_expiry_date是否结束,但我很难找出把它放在函数中的位置。试了几件事,但似乎打破了。请协助。

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 
+0

把它dbh_free($胸径)之间','和'收益$ id;'然后回来一个关于它为什么会中断的问题。 – Jasen

+0

确定编辑了代码,为什么它会中断? – Kilisi

+0

看起来像$ id_expiry_date未初始化。你有没有收到任何错误信息? – Jasen

回答

0

我真的不明白你的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"; 
} 
+0

所以这将是一个单独的功能?它不处理负载数组 – Kilisi

+0

使用你的例子 – Kilisi