anonymous No title
No License PHP
2020年10月25日
Copy
<!DOCTYPE html>
<HTML>
<head>
<meta charset="UTF-8">
<title>mysql</title>
</head>
<body>

<?php

$dsn = 'mysql:host=localhost;dbname=co_19_276_99sv_coco_com';
$user = 'co-19-276.99sv-c';
$password = 'A4jqacER';

#データベース接続
try {
    $dbh = new PDO($dsn, $user, $password);
    echo "接続成功\n";
} catch (PDOException $e) {
    echo "接続失敗: " . $e->getMessage() . "\n";
    exit();
}

#テーブル作成
$stmt = $dbh->query("CREATE TABLE user (id INT,name TEXT)");

#テーブル一覧表示
echo "テーブル一覧<br>";
  $st = $dbh->query('SHOW TABLES');
  while($ret = $st->fetch(PDO::FETCH_ASSOC)){
    var_dump($ret);
    echo '<br>';
  }
  echo "<br>";

#データー入力

$stmt=$dbh->prepare('INSERT INTO user (id,name)VALUES(:id,:name)');
$params = array(':id' => '3', ':name' => 'ishida');
$stmt->execute($params);
$res = $dbh-> query('SELECT * FROM user');
foreach( $res as $value ) {
  echo "id:"."$value[id]  ";
  echo "name:"."$value[name]";
  echo "<br>";
}

#データ編集
$stmt = $dbh->prepare("UPDATE user SET name=:name WHERE id=:id");
$params = array(':name' => '石田', ':id' => '1');
$stmt->execute($params);

#データ削除
$stmt = $dbh->prepare("DELETE FROM user WHERE id=:id");
$params = array(':id' => '2');
$stmt->execute($params);
?>

</body>
</html>
<!DOCTYPE html>
<HTML>
<head>
<meta charset="UTF-8">
<title>mysql</title>
</head>
<body>

<?php

$dsn = 'mysql:host=localhost;dbname=co_19_276_99sv_coco_com';
$user = 'co-19-276.99sv-c';
$password = 'A4jqacER';

#データベース接続
try {
    $dbh = new PDO($dsn, $user, $password);
    echo "接続成功\n";
} catch (PDOException $e) {
    echo "接続失敗: " . $e->getMessage() . "\n";
    exit();
}

#テーブル作成
$stmt = $dbh->query("CREATE TABLE user (id INT,name TEXT)");

#テーブル一覧表示
echo "テーブル一覧<br>";
  $st = $dbh->query('SHOW TABLES');
  while($ret = $st->fetch(PDO::FETCH_ASSOC)){
    var_dump($ret);
    echo '<br>';
  }
  echo "<br>";

#データー入力

$stmt=$dbh->prepare('INSERT INTO user (id,name)VALUES(:id,:name)');
$params = array(':id' => '3', ':name' => 'ishida');
$stmt->execute($params);
$res = $dbh-> query('SELECT * FROM user');
foreach( $res as $value ) {
  echo "id:"."$value[id]  ";
  echo "name:"."$value[name]";
  echo "<br>";
}

#データ編集
$stmt = $dbh->prepare("UPDATE user SET name=:name WHERE id=:id");
$params = array(':name' => '石田', ':id' => '1');
$stmt->execute($params);

#データ削除
$stmt = $dbh->prepare("DELETE FROM user WHERE id=:id");
$params = array(':id' => '2');
$stmt->execute($params);
?>

</body>
</html>

Output

Run
年末年始は機械学習・深層学習を勉強しませんか?
No one still commented. Please first comment.
年末年始は機械学習・深層学習を勉強しませんか?
広告
未経験から最短でエンジニアへの転職を目指すなら