LoginSignup
1
0

More than 5 years have passed since last update.

PHPでMySQLから一つだけデータを取り出す

Posted at
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="XXX">
<meta name="description" content="XXX">
<title>XXX</title>
<link rel="stylesheet" href="/main.css">
</head>

<body>
<?php
try {
//DB名、ユーザー名、パスワード
    $dsn = 'mysql:dbname=XXX;host=XXX';
    $user = 'XXX';
    $password = 'XXX';

    $PDO = new PDO($dsn, $user, $password); //MySQLのデータベースに接続
    $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $result = $PDO->query("SELECT id FROM contents WHERE id = 47329"); //MySQLのid列が47329の行だけを抽出
  while($row = $result->fetch(PDO::FETCH_ASSOC)){
    $rows .= $row['id']."</br>";
  }
} catch (PDOException $e) {
    exit('データベースに接続できませんでした。' . $e->getMessage());
}
?>

<?php echo $rows; ?>

</body>
</html>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0