Last active
January 10, 2020 05:11
-
-
Save s-hiroshi/2722778 to your computer and use it in GitHub Desktop.
PHPでmysql_connect関数を使いMySQLへ接続するサンプルです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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