如何检查php获取的是返回值还是空值

如何检查php获取的是返回值还是空值,php,pdo,Php,Pdo,若数据库中还并没有数据,我想将数据插入数据库,但若数据库中有数据,我想更新它 如何知道数据库中是否还没有数据或已经有数据 这是我的php代码,我尝试过countRow等,但总是返回未定义的方法 public function tambahCatatanDosenPA($no, $catatan, $semester) { $stmt = $this->conn->prepare("SELECT * FROM catatan_dosenpa WHERE no_user

若数据库中还并没有数据,我想将数据插入数据库,但若数据库中有数据,我想更新它

如何知道数据库中是否还没有数据或已经有数据 这是我的php代码,我尝试过countRow等,但总是返回未定义的方法

public function tambahCatatanDosenPA($no, $catatan, $semester) {

        $stmt = $this->conn->prepare("SELECT * FROM catatan_dosenpa WHERE no_user_id = ?");
        $stmt->bind_param("s", $no);
        $stmt->execute();
        $stmt->bind_result($no_isi, $no_user_id_isi, $catatan_isi, $semester_isi);
        $list_catatan = array();
        while ($stmt->fetch()) {
                $temp = array();
                $temp['no'] = $no_isi;
                $temp['no_user_id'] = $no_user_id_isi;
                $temp['catatan'] = $catatan_isi;
                $temp['semester'] = $semester_isi;
                array_push($list_catatan,$temp);
            }
            $stmt->close();
        if ($list_catatan.count() = 0) {

            $stmt = $this->conn->prepare("INSERT INTO catatan_dosenpa(no_user_id, catatan, semester) VALUES(?, ?, ?)");
            $stmt->bind_param("sss",$no, $catatan, $semester);
            $result = $stmt->execute();
            $stmt->close();
            return true;

        } else {

            $stmt = $this->conn->prepare("UPDATE catatan_dosenpa SET catatan = ? WHERE no_user_id = ? AND semester = ?");
            $stmt->bind_param("sss", $catatan,  $no, $semester);
            $result = $stmt->execute();
            $stmt->close();
            return true;
        }
    }

$list\u catatan.count()=0
在多个方面都是错误的。1. <代码>在PHP中连接。2.
count
将数组作为其参数。3.单个
=
分配。另外,您使用的是
mysqli
而不是PDO。您可以在查询中输入
count(*)
并返回值,但您最好在重复更新时使用
insert-into
,这样您就不需要计算了,DB会帮您做的。@user3783243我明白了,我会尝试在重复更新时使用insert-into,而不是您应该首先计数记录,正如@user3783243建议的那样,或者,如果您想保留代码,请使用count()php函数:if(count($list_catatan)==0)@user3783243我已尝试将此$stmt=$this->conn->prepare(“插入catatan_dosenpa(无用户id,catatan,学期)值(?,?)更改为重复键更新catatan=?”;$stmt->bind_param(“ssss”、$no、$catatan、$sement、$catatan);bind_param出现错误,你能告诉我出了什么问题吗?但实际上我已经通过更改count()解决了这个问题,感谢大家,但仍然想知道我的插入到重复密钥更新中的错误是什么,以供研究