为PHP pdo选择bind_param

时间:2019-07-31 19:33:33

标签: php pdo

我已经在php pdo中创建了delete语句。我需要知道我的代码的bind_param查询。我使用了一个不会崩溃的bind_param。

PHP7

public function deleteRecord($table,$pk,$id){
        if($table == "categories"){
            $pre_stmt = $this->con->prepare("SELECT ".$id." FROM categories WHERE parent_cat = ?");
            $pre_stmt->bind_param("i",$id);
            $pre_stmt->execute();
            $result = $pre_stmt->get_result() or die($this->con->error);
            if ($result->num_rows > 0) {
                return "DEPENDENT_CATEGORY";
            }else{
                $pre_stmt = $this->con->prepare("DELETE FROM ".$table." WHERE ".$pk." = ?");
                $pre_stmt->bind_param("i",$id);
                $result = $pre_stmt->execute() or die($this->con->error);
                if ($result) {
                    return "CATEGORY_DELETED";
                }
            }

1 个答案:

答案 0 :(得分:0)

我在上面纠正了错误,然后在下面错误了  公共函数getAllRecord($ table){

    $result = $this->con->prepare("SELECT * FROM ".$table);
    $result->execute();
    $results = $result->fetchAll();
    $rows = array();
    if ($results->rowCount > 0) {
        while($row = $results->fetch(PDO::FETCH_ASSOC)){
            $rows[] = $row;
        }
        return $rows;
    }
    return "NO_DATA";
}

错误:注意:尝试获取第68行上C:\ xampp \ htdocs \ inv_project \ public_html \ includes \ DBOperation.php中非对象的属性