-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.php
95 lines (82 loc) · 2.14 KB
/
test1.php
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
87
88
89
90
91
92
93
94
95
<?php
/*MySQL Code*/
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
echo "<br>";
//Check if the Database Exists
$sql = "CREATE DATABASE IF NOT EXISTS myDB";
//$sql = "CREATE DATABASE dbname";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully!";
echo "<br>";
$conn = new mysqli($servername, $username, $password, $dbname);
} else {
echo "Error creating database: " . $conn->error;
}
// sql to create table
if ($conn->query($sql) === TRUE) {
echo "Table Exists";
echo "<br>";
} else {
// create a new table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
}
// check again
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully!";
echo "<br>";
} else {
echo "Error creating table: " . $conn->error;
echo "<br>";
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully!";
echo "<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//Select Data from Table
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
/*E-mail Code
error_reporting(-1);
ini_set('display_errors', 'On');
$headers = array("From: from@example.com",
"Reply-To: replyto@example.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$didhappen = mail('mtang.test@gmail.com', 'test', 'test', $headers);
if($didhappen) {
echo 'true';
} else {
echo 'false';
}
*/
?>