【发布时间】:2018-09-04 20:03:15
【问题描述】:
我在 PHP 中有一个 sql 请求,这个查询的结果放在类变量中 但是当 tis 请求没有返回结果时,我想在变量中有一个空值,目前我返回了一个 false 布尔值。
你能帮帮我吗? 谢谢
<?php
class test
{
const table_name = 'test_table';
protected $Id;
protected $Nom;
public static function byId($id)
{
$values = array
(
':Id' => $id,
);
$sql = 'SELECT *FROM '.self::table_name.' WHERE Id=:Id';
$QueryPrep = self::getInstance()->prepare($sql);
if(empty($param))
{
$resultQuery = $QueryPrep->execute();
}
else
{
$resultQuery = $QueryPrep->execute($values);
}
if ($resultQuery !=false)
{
$res = $resultQuery->fetchObject(__CLASS__);
return $res;
}
else
{
return false;
}
}
public function get_Id()
{
return $this->Id;
}
}
$cmd_device_id = null;
$cmdDeviceObject = new test();
$cmd_device = $cmdDeviceObject->byId(999);
$cmd_device_id = $cmd_device->get_Id();
echo "cmd_device_id = ".var_dump($cmd_device_id);
?>
【问题讨论】:
-
所以只返回 null 而不是 false。
else { return null; } -
$params我想是为了作为一个函数参数?并且不存在于任何地方。虽然它对执行没有太大影响 -
如果你整理好你的逻辑,你就会明白发生了什么,我们也会
标签: php