이번 수업에서는 RDS와 PHP를 연동하는 방법을 알아봅니다.
선수수업
개발준비
PHP와 MySQL(MariaDB) 연동
Read Replication
예제
db.php
<?php $write = mysqli_connect('', '', '', 'o2'); $read = mysqli_connect('', '', '', 'o2');
rds.php
<!DOCTYPE> <html> <head><meta charset="utf-8"></head> <body> <form action="rds_receiver.php" method="post"> <p> 제목 : <input type="text" name="title"> </p> <p> 본문 : <textarea name="description"> </textarea> </p> <p><input type="submit"></p> </form> <ul> <?php include('db.php'); $result = mysqli_query($read, "SELECT * FROM topic"); while($row = mysqli_fetch_assoc($result)){ $title = mysqli_real_escape_string($read, $row['title']); print("<li>{$title}</li>"); } ?> </ul> </body> </html>
rds_receiver.php
<?php include('db.php'); $title = mysqli_real_escape_string($write, $_POST['title']); $description = mysqli_real_escape_string($write, $_POST['description']); $sql = "INSERT INTO topic (title, description, author, created) VALUES('{$title}', '{$description}', 'egoing', NOW())"; mysqli_query($write, $sql); header('Location: rds.php'); ?>