MySQL & PHP 실습 (2)

yurison·2022년 12월 30일
0
post-custom-banner
<?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) {
      $sql = "SELECT * FROM table_name WHERE id=" . $_GET['id'];
      $result = mysqli_query($conn, $sql);
      $row = mysqli_fetch_assoc($result);
      echo '<h2>' . $row['title'] . '</h2>';
      echo $row['description'];
    }
    ?>
  </article>
</body>

</html>
post-custom-banner

0개의 댓글