라이브러리

[PHP] TableDelete::__construct - TableDelete 생성자




PHP TableDelete::__construct


PHP의 TableDelete 클래스는 데이터베이스 테이블에서 데이터를 삭제하는 데 사용됩니다. 이 클래스의 `__construct` 메서드는 생성자로, 클래스의 초기화 작업을 수행합니다.

# TableDelete::__construct 메서드의 역할


TableDelete::__construct 메서드는 다음 작업을 수행합니다.

1. 테이블 이름: 테이블 이름을 설정합니다.
2. 조건: 삭제할 데이터의 조건을 설정합니다.

# 예제


#hostingforum.kr
php

// 데이터베이스 연결 설정

$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');



// TableDelete 클래스 인스턴스 생성

$tableDelete = new TableDelete($db, 'mytable');



// 삭제할 데이터의 조건 설정

$tableDelete->where('id', 1);



// 데이터 삭제

$tableDelete->delete();



// 데이터베이스 연결 닫기

$db = null;



# TableDelete 클래스의 예제


#hostingforum.kr
php

class TableDelete {

    private $db;

    private $tableName;

    private $where;



    public function __construct($db, $tableName) {

        $this->db = $db;

        $this->tableName = $tableName;

    }



    public function where($column, $value) {

        $this->where = "WHERE $column = '$value'";

        return $this;

    }



    public function delete() {

        $query = "DELETE FROM $this->tableName $this->where";

        $stmt = $this->db->prepare($query);

        $stmt->execute();

    }

}



# 사용 예제


#hostingforum.kr
php

// 데이터베이스 연결 설정

$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');



// TableDelete 클래스 인스턴스 생성

$tableDelete = new TableDelete($db, 'mytable');



// 삭제할 데이터의 조건 설정

$tableDelete->where('id', 1);



// 데이터 삭제

$tableDelete->delete();



// 데이터베이스 연결 닫기

$db = null;



# 참고


* PDO (PHP Data Objects) 클래스는 데이터베이스와 상호 작용하는 데 사용됩니다.
* `prepare` 메서드는 SQL 쿼리를 준비하고, `execute` 메서드는 쿼리를 실행합니다.
* `WHERE` 절은 삭제할 데이터의 조건을 설정합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 87 페이지

검색

게시물 검색