기존 코드
<?php
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, "database_name");
$result = mysqli_query($conn, "SELECT * FROM table_name");
?>
개선 코드
<?php
require("config/config.php");
require("lib/db.php");
$conn = db_init($config["host"], config["duser"], config["dpw"], config["dname"]);
$result = mysqli_query($conn, "SELECT * FROM <table_name>");
?>
<?php
function db_init($host, $duser, $dpw, $dname) {
ㅤㅤ$conn = mysqpli_connect($host, $duser, $dpw);
ㅤㅤmysqli_select_db($conn, $dname);
ㅤㅤreturn $conn;
}
?>
<?php
$config = array(
ㅤㅤ"host"=>"localhost",
ㅤㅤ"duser"=>"root",
ㅤㅤ"dpw"=>"111111",
ㅤㅤ"dname"=>"<db_name>"
);
?>