每次运行php文件时我都调用了我的更新sql,它返回true语句,但是记录无法完美更新。我想知道我的代码出错了吗?请帮助我,我将不胜感激。提前谢谢。
这是我在event-listing.php
中的php代码:
$update_event_list = $event->updateeventlist($type = 1);
这是我在Event.inc.php
中的sql语句:
function updateeventlist($type){
global $db;
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt)){
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
}
}
}
return true;
}
我试图回应声明并返回我想要的真实陈述。
答案 0 :(得分:2)
您需要在更新查询后添加execute
函数。
$rs = $db->Execute($updateEvent);
答案 1 :(得分:1)
更新查询后缺少查询执行
function updateeventlist($type){
global $db;
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt)){
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
$db->Execute($updateEvent);
}
}
}
return true;
}
答案 2 :(得分:1)
以下是我在您的代码中观察到的一些要点: -
即使您没有更新或获取任何记录,您仍然会得到“true”,因为如果失败则没有条件指定何时返回false。
$stmt = "SELECT * FROM "._CONST_TBL_EVENT." WHERE type = ".$type;
if($rs = $db->Execute($stmt))
{
if( $rs has atleast one row rows )
{
while($rsa = $rs->FetchRow())
{
if($rsa['start_date'] < strtotime("now")){
$updateEvent = "UPDATE "._CONST_TBL_EVENT." SET type = 2 WHERE id = ".$rsa['id'];
$db->Execute($updateEvent); // this line was missing in you code
}
}
}
else
{
return false;
// $rsa has empty rows
}
}
else // execution of query fails for any reason
{
return false;
}