PDO/PHP - Check if row exist

后端 未结 3 1135
面向向阳花
面向向阳花 2020-11-30 06:03

I want to have a condition incase the row doesn\'t exist at all.

$stmt = $conn->prepare(\'SELECT * FROM table WHERE ID=?\');
$stmt->bindParam(1, $_GET[         


        
3条回答
  •  盖世英雄少女心
    2020-11-30 06:03

    Heres what I use in my object classes:

    function exists_by_id () {
        // check if object exists by id
        $stm = DB::$pdo->prepare('select count(*) from `table` where `column`=:column');
        $stm->bindParam(':column', $this->column);
        $stm->execute();
        $res = $stm->fetchColumn();
    
        if ($res > 0) {
            return true;
        }
        else {
            return false;
        }
    }
    

提交回复
热议问题