Knowee
Questions
Features
Study Tools

Write a PHP program to connect to a database and create a database with employee table.

Question

Write a PHP program to connect to a database and create a database with employee table.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple PHP program to connect to a MySQL database and create a database with an employee table.

<?php
$servername = "localhost";
$username = "username";
$password = "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 myDB";
if ($conn->query($sql) === TRUE) {
  echo "Database created successfully";
} else {
  echo "Error creating database: " . $conn->error;
}

// Select database
mysqli_select_db($conn, 'myDB');

// sql to create table
$sql = "CREATE TABLE Employee (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
  echo "Table Employee created successfully";
} else {
  echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Please replace "localhost", "username", and "password" with your actual database server details. This script will create a database named "myDB" and a table named "Employee" with five fields: id, firstname, lastname, email, and reg_date.

This problem has been solved

Solution 2

Sure, here is a simple PHP program that connects to a MySQL database and creates a database with an employee table.

<?php
$servername = "localhost";
$username = "username";
$password = "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 myDB";
if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";
} else {
    echo "Error creating database: " . $conn->error;
}

// Select database
mysqli_select_db($conn, 'myDB');

// sql to create table
$sql = "CREATE TABLE Employee (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table Employee created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Please replace "username" and "password" with your actual MySQL username and password. This script will create a new database named "myDB" and a table named "Employee" with five fields: id, firstname, lastname, email, and reg_date.

This problem has been solved

Similar Questions

Create a web page to retrieve personal details of an employee when his first name or last name is given as an input using a web page. First create an Employee table inside company_A database which contains the following fields.IDFirst nameLast nameDate of BirthDesignationStart DateSalaryBranch

Create Table Employee with attributes firstName,LastName,SSN,Address,Salary,Birthday,Sex,SupervisorSSN,DepartmentNo

PHP syntax to establish database connection

write sql query for the following:1. create database - emp2.create table - employee with 4 columns3.To see table structure

Problem StatementIn a company's Employee Management System, a database table named "Employees" that stores information about employees, including their EmployeeID, EmployeeName, Department, and Salary.The HR department needs a mechanism to delete employee records from the system when an employee leaves the company or for any other valid reason. To address this requirement, you are tasked with creating a stored procedure named "DeleteEmployee," which takes an EmployeeID as input and deletes the corresponding employee record from the "Employees" table.The following table is already created, and the records are inserted into the table.TABLE: Employees EmployeeID INT PRIMARY KEY, EmployeeName VARCHAR(50), Department VARCHAR(50), Salary DECIMAL(10, 2)Prepopulated Records:Input format :The input records are already prepopulated, as given in the problem statement.Output format :The stored procedure deletes the employee record based on the provided EmployeeID.The output should display the employee details after deletion as shown below.EmployeeID EmployeeName Department Salary1 John Doe IT 60000.002 Jane Smith HR 55000.003 Bob Johnson Marketing 70000.004 Alice Williams Finance 80000.00EmployeeID EmployeeName Department Salary1 John Doe IT 60000.003 Bob Johnson Marketing 70000.004 Alice Williams Finance 80000.00Refer to the sample output for the column headers.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.