forked from nvogiatzis/PHP_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatestartingdb.php
More file actions
86 lines (74 loc) · 2.59 KB
/
createstartingdb.php
File metadata and controls
86 lines (74 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users_DB";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully<br>";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE $dbname";
// use exec() because no results are returned
$conn->exec($sql);
echo "Database <b>$dbname</b> created successfully<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table
$sql = "CREATE TABLE users (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)";
// use exec() because no results are returned
$conn->exec($sql);
echo "Table <b>users</b> created successfully<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO users (username, password)
VALUES (:username, :password)");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
// insert first admin
$username = "webadmin7";
$password = password_hash("webadmin7", PASSWORD_DEFAULT);
$stmt->execute();
// insert second admin
$username = "webadmin10";
$password = password_hash("webadmin10", PASSWORD_DEFAULT);
$stmt->execute();
echo "Two new admin records have been created successfully<br>";
echo "1st admin -- username: webadmin7, passowrd: webadmin7<br>";
echo "2nd admin -- username: webadmin10, passowrd: webadmin10<br>";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
?>