Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active January 10, 2020 05:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/2722778 to your computer and use it in GitHub Desktop.
Save s-hiroshi/2722778 to your computer and use it in GitHub Desktop.
PHPでmysql_connect関数を使いMySQLへ接続するサンプルです。
<?php
// exampleデータベースにfield1, field2を持つtable1テーブルを作成済みの前提
// データベース接続情報
$url = "localhost";
$user = "root";
$password = "";
$database = "example";
// データベース接続処理
$connect = mysql_connect($url,$user,$password) or die("can't connect");
$db = mysql_select_db($database,$connect) or die("can't Select database");
$sql = "SELECT * FROM table1";
$result = mysql_query($sql, $connect) or die("can't submit SQL");
?>
<html>
<head>
<title>Test Connection from PHP to MySQl</title>
</head>
<body>
<h1>テーブルの中身</h1>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<p>";
echo htmlspecialchars($row["field1"]);
echo "<br />";
echo htmlspecialchars($row["field2"]);
echo '</p>';
}
mysql_free_result($result);
mysql_close($connect) or die("can't closed");
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment