-3
PDO 연결을 사용하여 CRUD하고 싶습니다.PDO 연결을 사용하여 CRUD하는 방법은 무엇입니까?
msql_query()를 사용하여 삽입 업데이트 및 삭제를 만드는 방법을 알고 있지만 PDO 연결을 사용하여이를 수행하는 방법을 알지 못합니다.
PDO 연결을 사용하여 CRUD하고 싶습니다.PDO 연결을 사용하여 CRUD하는 방법은 무엇입니까?
msql_query()를 사용하여 삽입 업데이트 및 삭제를 만드는 방법을 알고 있지만 PDO 연결을 사용하여이를 수행하는 방법을 알지 못합니다.
아래는 포스트 질문하기 전에 구글에서 몇 가지 조사를하시기 바랍니다
class connection{
public $cnn;
public function __construct(){
$host = 'localhost';
$db_name = "db_name";
$username = "db_username";
$password = "db_password";
try {
$this->cnn = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function select($query){ //this function is created for get data
$result = $this->cnn->query($query);
return $result->fetchAll(PDO::FETCH_ASSOC);
}
public function insert($query){ //this function is created for insert data. it will be return last inserted id.
$this->cnn->exec($query);
return $this->cnn->lastInsertId();
}
public function update($query){ //this function is created for update data and it will be return effected rows (which are updated)
return $this->cnn->exec($query);
}
public function delete($query){ // this function is use to delete data.
return $this->cnn->exec($query);
}
}
$action = new connection;
$result = $action->select("select * from table_name");
print_r($result);
$result = $action->insert("insert into table_name set column_1 = 'first_value', column_2='second_value'");
$result = $action->update("update table_name set column_1 = 'first_value', column_2='second_value' where id=1");
$result = $action->delete("delete from table_name where id=1");
ohk을 수행 어떤 작업을 보여주십시오 .. 고마워요. –
의 예입니다, 튜토리얼의 많은이 주제에 사용할 수있다 – theinarasu
이미 – ntohl