0% found this document useful (0 votes)
18 views3 pages

HTML Lab Code

ip

Uploaded by

asmeradagne
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
18 views3 pages

HTML Lab Code

ip

Uploaded by

asmeradagne
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

Create connection....................

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$con = mysqli_connect("localhost", "root", "");

// Check connection
if($con === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Print host information


echo "Connect Successfully. Host info: " . mysqli_get_host_info($con);
?>
Create Database..............................
<?php
require('connection2.php');
// Attempt create database query execution
$sql = "CREATE DATABASE HRM_DB";
if(mysqli_query($con, $sql)){
echo "Database created successfully";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
Create Table...............................
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$con = mysqli_connect("localhost", "root", "", "HRM_DB");

// Check connection
if($con === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt create table query execution
$sql = "CREATE TABLE employee(emp_id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,emp_name VARCHAR(20) NOT NULL, emp_address VARCHAR(20) NOT NULL,
emp_salary INT NOT NULL)";
if(mysqli_query($con, $sql)){
echo "Table created successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
Insert Value to Table..........................
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "HRM_DB");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql = "INSERT INTO employee(emp_id,emp_name,emp_address, emp_salary)VALUES (127,
'Hanna', 'Jimma', 3000)";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Drop Database.................................
<?php
require('connection2.php');
// Attempt drop database query execution
$sql = "DROP DATABASE HRM_DB";
if(mysqli_query($con, $sql)){
echo "Database deleted successfully";
} else{
echo "ERROR: Could not able to delete $sql. " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
Drop Table.......................................
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "HRM_DB");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt drop table query execution
$sql = "DROP TABLE persons";
if(mysqli_query($link, $sql)){
echo "Table deleted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Delete table row....................................
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "HRM_DB");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt delete query execution
$sql = "DELETE FROM employee WHERE emp_id=124";
if(mysqli_query($link, $sql)){
echo "Records were deleted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Update Value in Table.......................................
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "HRM_DB");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt update query execution
$sql = "UPDATE employee SET emp_salary=2000 WHERE emp_id=123";
if(mysqli_query($link, $sql)){
echo "Records were updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Select value from table......................................
<?php
$username="username";
$password="password";
$hostname="localhost";
$database="School";
$dbhandle=mysqli_connect($hostname, $username, $password,$database);
if($dbhandle===false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$exeobj=mysqli_query($dbhandle,"SELECT ID, Name, City FROM StudentMst" );
while ($qry=mysqli_fetch_array($exeobj)) {

echo "ID:" .$row{'ID'}." Name:".$row{'Name'}." City: ". $row{'City'}."<br>";

}
mysqli_close($dbhandle);
}

You might also like