Php MySql Code to configure localhost and Remote Server config.php
Do you want to configure your localhost server in a way that you do not need to access manually to phpmyadmin and a configuration file to set up database and all necessary tables easily. Here is a php file, copy this file and run this file after starting localhost server and MySql database and the database and tables will be created automatically.
<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
// Create database
$sql = “CREATE DATABASE IF NOT EXISTS students”;
if ($conn->query($sql) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating database: ” . $conn->error;
}
$sql2 = “CREATE TABLE IF NOT EXISTS students.stdata
(
UID bigint(20) NOT NULL AUTO_INCREMENT,
Roll varchar(100),
Name varchar(50),
Address varchar(50),
Gender varchar(20),
Password varchar(20),
Phone varchar(20),
Image varchar(50),
Time bigint(20),
Staff varchar(50),
Fee int(10),
Paid int(10),
PRIMARY KEY (UID)
)”;
if ($conn->query($sql2) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stdata: ” . $conn->error;
}
$sql22 = “CREATE TABLE IF NOT EXISTS students.staff
(
id int(11) NOT NULL AUTO_INCREMENT,
Username varchar(20),
Password varchar(20),
Ref varchar(20),
Verify varchar(50),
Time bigint(20),
PRIMARY KEY (id)
)”;
if ($conn->query($sql22) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating staff: ” . $conn->error;
}
$sql3 = “CREATE TABLE IF NOT EXISTS students.stexam
(
EID bigint(20) NOT NULL AUTO_INCREMENT,
ExamID varchar(100),
Ename varchar(50),
Emarks varchar(20),
Time bigint(20),
PRIMARY KEY (EID)
)”;
if ($conn->query($sql3) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stexam: ” . $conn->error;
}
$sql4 = “CREATE TABLE IF NOT EXISTS students.stmark
(
NID bigint(20) NOT NULL AUTO_INCREMENT,
Roll varchar(100),
ExamID varchar(50),
Gmarks varchar(20),
Time bigint(20),
PRIMARY KEY (NID)
)”;
if ($conn->query($sql4) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stmark: ” . $conn->error;
}
$dbname = “students”;
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
?>
For the remote server, firstly you have to make a database name “students”, then copy the following code:
<?php
$servername = “yourdomain.com”;
$username = “database-username”;
$password = “database-user-password”;
$dbname = “students”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
$sql2 = “CREATE TABLE IF NOT EXISTS students.stdata
(
UID bigint(20) NOT NULL AUTO_INCREMENT,
Roll varchar(100),
Name varchar(50),
Address varchar(50),
Gender varchar(20),
Password varchar(20),
Phone varchar(20),
Image varchar(50),
Time bigint(20),
Staff varchar(50),
Fee int(10),
Paid int(10),
PRIMARY KEY (UID)
)”;
if ($conn->query($sql2) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stdata: ” . $conn->error;
}
$sql22 = “CREATE TABLE IF NOT EXISTS students.staff
(
id int(11) NOT NULL AUTO_INCREMENT,
Username varchar(20),
Password varchar(20),
Ref varchar(20),
Verify varchar(50),
Time bigint(20),
PRIMARY KEY (id)
)”;
if ($conn->query($sql22) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating staff: ” . $conn->error;
}
$sql3 = “CREATE TABLE IF NOT EXISTS students.stexam
(
EID bigint(20) NOT NULL AUTO_INCREMENT,
ExamID varchar(100),
Ename varchar(50),
Emarks varchar(20),
Time bigint(20),
PRIMARY KEY (EID)
)”;
if ($conn->query($sql3) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stexam: ” . $conn->error;
}
$sql4 = “CREATE TABLE IF NOT EXISTS students.stmark
(
NID bigint(20) NOT NULL AUTO_INCREMENT,
Roll varchar(100),
ExamID varchar(50),
Gmarks varchar(20),
Time bigint(20),
PRIMARY KEY (NID)
)”;
if ($conn->query($sql4) === TRUE) {
//echo “Database created successfully”;
} else {
echo “Error creating stmark: ” . $conn->error;
}
$dbname = “students”;
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
?>