MySQL & PHP 실습 (1)

yurison·2022년 12월 29일
0
post-thumbnail

<?php
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, "database_name");
$result = mysqli_query($conn, "SELECT * FROM table_name");
while ($row = mysqli_fetch_assoc($result)) {
  echo $row['id'];
  echo $row['title'];
  echo $row['author'];
  echo "<br />";
}
?>

완성 코드↓

<?php
$conn = mysqli_connect("localhost", "root", "111111");
mysqli_select_db($conn, "database_name");
$result = mysqli_query($conn, "SELECT * FROM table_name");
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="http://localhost/project_php/style.css" />
  <title>PHP Project</title>
</head>

<body id="target">
  <header>
    <h1>
      <a href="http://localhost/project_php/index.php">JavaScript</a>
    </h1>
  </header>
  <nav>
    <ul>
      <?php
      while ($row = mysqli_fetch_assoc($result)) {
        echo "<li><a href='http://localhost/index.php?id=" . $row['id'] . "'>" . $row['title'] . "</a></li>" . "\n";
      }
      ?>
    </ul>
  </nav>
  <div>
    <input type="button" value="white" onclick="document.getElementById('target').className='white'" />
    <input type="button" value="black" onclick="document.getElementById('target').className='black'" />
  </div>
  <article>
    <?php
    if (empty($_GET['id']) == false) {
      echo file_get_contents($_GET['id'] . ".txt");
    }
    ?>
  </article>
</body>

</html>

0개의 댓글