๐ ๊ฐ์ข ์์ ์ ์ฐธ๊ณ ์ฌํญ
์ด ๋ด์ฉ์ ๊ตฌ์ง ์ดํดํ๋ ค ํ์ง ์์ผ์ ๋ ๋ฉ๋๋ค.
- PHP7 ์ด์๋ถํฐ๋ ๋ ์ด์ MySQL_* ๊ด๋ จ ํจ์๋ฅผ ์ง์ํ์ง ์์ต๋๋ค.
- ๋ฐ๋ผ์ MySQLi ๋ฅผ ์ด์ฉํ์ฌ ํ๋ก๊ทธ๋๋ฐ ํด์ผ ํฉ๋๋ค.
- PHP5 ๋ฒ์ ์์ PHP7๋ก ์ ๊ทธ๋ ์ด๋ ์์ ์ด๋ผ๋ฉด MySQL_* ํจ์๋ฅผ ์์ ํ์ธ์.
- PHP Data Objects ๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ ๋ํ ์ข์ ๋์์ฑ ์ ๋๋ค...
๐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธํ
mysql๊ณผ php๋ฅผ ์ฐ๋ํ๋ ค๋ฉด mysql์ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ ์ด๋ธ์ด ์์ด์ผ ํฉ๋๋ค.
create database php;
์ ๋ช ๋ น์ด๋ php ๋ผ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ง๋ค๋ผ๋ ๋ช ๋ น ์ ๋๋ค.
USE php;
php ํ ์ด๋ธ์ ์ด์ฉํ๋ผ๋ ์๋ฏธ ์ ๋๋ค.
CREATE TABLE `user` (
`idx` int(11) NOT NULL auto_increment,
`id` varchar(100) NOT NULL,
`password` varchar(20) NOT NULL,
`name` varchar(20) NOT NULL,
`created` date DEFAULT NULL,
PRIMARY KEY (`idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
user ๋ผ๋ ํ ์ด๋ธ์ ๋ง๋ค๊ณ
user ํ ์ด๋ธ์
idx ํ๋๋ฅผ int ํ์์ผ๋ก ์ค๋ณต๋์ง ์๊ณ ์๋์ผ๋ก ๋ฒํธ๊ฐ ๋งค๊ฒจ์ง๋ ํ๋๋ก ๋ง๋ค๊ณ ,
id ํ๋๋ฅผ varchar ํ์์ผ๋ก ์ค๋ณต๋์ง ์๊ฒ ๋ง๋ค๊ณ ,
password ํ๋๋ฅผ varchar ํ์์ผ๋ก ์ค๋ณต๋์ง ์๊ฒ ๋ง๋ค๊ณ ,
name ํ๋๋ฅผ varchar ํ์์ผ๋ก ์ค๋ณต๋์ง ์๊ฒ ๋ง๋ค๊ณ ,
created ํ๋๋ฅผ date ํ์์ผ๋ก ๋ง๋ค๊ณ , ๊ธฐ๋ณธ๊ฐ์ผ๋ก NULL์ ๋ฃ๋๋ค.
user ํ ์ด๋ธ์ ๊ธฐ๋ณธํค๋ idx๋ก ํ๊ณ ,
InnoDB ์์ง์ ์ฌ์ฉํ๊ณ , ๊ธฐ๋ณธ ์ธ์ฝ๋ฉ์ utf8 ํ์์ผ๋ก ํ๋ค.
์ด๋ฐ ๋ป์ ๋๋ค (ํ๋ฆฐ ๋ถ๋ถ ์์ผ๋ฉด ์ง์ ํด์ฃผ์ธ์)
์ ์ด์ ์ฝ๋๋ก ๊ฐ์....
db.php ๋ผ๋ ํ์ผ์ ๋ง๋ค์ด ์ค๋๋ค.
๐ ์ฝ๋ ์์ฑ
db.php
<?php
session_start();
$conn = mysqli_connect("localhost", "root","your_db_password", "php");
$conn->set_charset("utf8");
?>
index.php
<form method="POST" action="method.php">
<input type="text" name="id" placeholder="ID" />
<input type="password" name="password" placeholder="Password" />
<input type="text" name="name" placeholder="Your Name" />
<input type="submit" />
</form>
method.php
<?php
include "db.php";
$id = $_POST['id'];
$pw = $_POST['password'];
$name = $_POST['name'];
$date = date("Y-m-d", time());
$result = mysqli_query($conn, "INSERT INTO user(id, password, name, created)VALUES('".$id."','".$pw."','".$name."','".$date."')") or die ("์์์๋ ์ค๋ฅ");
?>
๊ธฐ๋ฅ์ด ์ค์ํ๊ธฐ ๋๋ฌธ์ ๊น์ ์ค๋ช ์ ํ์ง ์๊ฒ ์ต๋๋ค.
์ ๋ฒ ์๊ฐ์ ๋ฐฐ์ ๋ post ๋ฐฉ์ ์ ์ก์ ์ฌ์ฉํด์,
id, password, name ์ method.php๋ก ์ ์กํฉ๋๋ค.
mysqli_connect ํจ์๋ MySql๊ณผ PHP๋ฅผ ์ฐ๋์์ผ์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
mysqli_query ํจ์๋ SQL ์ ์คํ์์ผ์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
$conn์ db.php์์ ์ ์ํด๋ ๋ณ์ ์ ๋๋ค.
์ด์ insertํ ๊ฐ์ select(์กฐํ) ํด๋ณด๊ฒ ์ต๋๋ค.
select.php ํ์ผ์ ์์ฑํด ์ค๋๋ค.
์ฐ์ db.php ๋ฅผ ์๋์ฒ๋ผ ์์ ํด ์ค๋๋ค.
<?php
session_start();
$conn = mysqli_connect("localhost", "root","root", "php");
$conn->set_charset("utf8");
function mq($sql) {
global $conn;
return $conn->query($sql);
}
?>
๊ทธ ๋ค์, index.php๋ฅผ ์์ ํด ์ค๋๋ค.
<form method="POST" action="method.php">
<input type="text" name="id" placeholder="ID" />
<input type="password" name="password" placeholder="Password" />
<input type="text" name="name" placeholder="Your Name" />
<button type="submit">์ ์ถ</button>
<a href="select.php"><button type="button">ํ๋ ์กฐํํ๋ฌ ๊ฐ๊ธฐ</button></a>
</form>
๊ทธ ๋ค์ select.php๋ฅผ ์์ฑ ํด ์ค๋๋ค.
<?php
include "db.php";
?>
<table>
<th>idx</th>
<th>id</th>
<th>password</th>
<th>name</th>
<th>created</th>
<?php
$sql = mq("select * from user");
while($data = $sql->fetch_array()) {
?>
<tr>
<td><?php echo $data["idx"]; ?></th>
<td><?php echo $data["id"]; ?></th>
<td><?php echo $data["password"]; ?></th>
<td><?php echo $data["name"]; ?></th>
<td><?php echo $data["created"]; ?></th>
</tr>
<?php
}
?>
๐ ์ค๋์ ๊ฒฐ๊ณผ
'Web > PHP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ PHP์์ ๋ฆฌ์ผ ํ์๊ฐ์ / ๋ก๊ทธ์ธ ๊ตฌํํ๊ธฐ (#7) (1) | 2020.04.18 |
---|---|
โ Let's Leaning PHP! - ์งํ ๋ก๊ทธ์ธ ๋ง๋ค๊ธฐ(?) (#5) (0) | 2020.03.31 |
โ Let's Leaning PHP! - ๊ฐ ์ ์กํ๊ธฐ (#4) (0) | 2020.03.31 |
โ Let's Leaning PHP! - ๋ณ์ ๋ค๋ฃจ๊ธฐ (#3) (0) | 2020.03.31 |
โ Let's Leaning PHP! - echo ๋ค๋ฃจ๊ธฐ (#2) (0) | 2020.03.27 |
๋๊ธ