0% found this document useful (0 votes)
4 views

Database + File Handling

The document provides PHP code examples for basic database operations using MySQL, including creating a database, creating a table, inserting, updating, and deleting records. It also includes file handling operations such as writing, reading, creating directories, and renaming files. Additionally, it demonstrates form handling for performing basic arithmetic operations (addition, subtraction, multiplication) based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Database + File Handling

The document provides PHP code examples for basic database operations using MySQL, including creating a database, creating a table, inserting, updating, and deleting records. It also includes file handling operations such as writing, reading, creating directories, and renaming files. Additionally, it demonstrates form handling for performing basic arithmetic operations (addition, subtraction, multiplication) based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Database Operations (MySQL)

Database Create

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "INC";

$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
echo "fail to connect";
}

$sql = "create database INC";

if ($conn->query($sql) == true) {
echo "Database created successfully";
} else {
echo "fail to create database";
}
?>
Create Table

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "INC";

$conn = mysqli_connect($servername, $username, $password, $database);

if (!$conn) {
echo "fail to connect";
}

$sql = "create table student(id int, name varchar(50), email varchar(50))";

if ($conn->query($sql) == true) {
echo "table create successfully";
} else {
echo "fail to create table";
}
?>
Insert values

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "INC";

$conn = mysqli_connect($servername, $username, $password, $database);

if (!$conn) {
echo "fail to connect";
}

$sql = "insert into student values(2, 'Moin', '[email protected]')";

if ($conn->query($sql) == true) {
echo "Data has been inserted successfully";
} else {
echo "fail to insert data";
}
?>
Update values

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "INC";

$conn = mysqli_connect($servername, $username, $password, $database);

if (!$conn) {
echo "fail to connect";
}

$sql = "Update student set name='Moin Khan' where id=2";

if ($conn->query($sql) == true) {
echo "Data has been updated successfully";
} else {
echo "fail to update data";
}
?>
Delete values

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database = "INC";

$conn = mysqli_connect($servername, $username, $password, $database);

if (!$conn) {
echo "fail to connect";
}

$sql = "delete from student where id=2";

if ($conn->query($sql) == true) {
echo "Data has been deleted successfully";
} else {
echo "fail to delete data";
}
?>
File Handling (PHP)
File writing
<?php
$file = fopen("text.txt", "w");
$data = "Hello World";

fwrite($file, $data);
fclose($file);
?>

File reading

<?php
$file = fopen("text.txt", "r");

while (!feof($file)) {
$data = fgetc($file); // eta use korle character by character read korbe
$data = fgets($file); // eta use korle line by line read korbe
// ekhane jekono ekta use korle hobe, tobe question e jodi alada kore bole
line by line read korte tahole fgets() use korte hobe
echo $data;
}
fclose($file);
?>
Create Folder/Directory

<?php
$file = fopen("text.txt", "r");

mkdir("New Folder");

fclose($file);
?>
Rename File

<?php
$file = fopen("text.txt", "r");

rename("text.txt", "NewName.txt");

fclose($file);
?>
Important form handling (add, sub, mul)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>

<form method="get">
<label>Enter the 1st Number: </label>
<input type="number" name="first" placeholder="Enter the number...">
<br><br>

<label>Enter the 2nd Number: </label>


<input type="number" name="second" placeholder="Enter the number...">
<br><br>

<input type="submit" name="add" value="add">


<input type="submit" name="sub" value="sub">
<input type="submit" name="mul" value="mul">
<br><br>

</form>

<?php

if(isset($_GET['add'])) {
$num1 = $_GET['first'];
$num2 = $_GET['second'];
$res = $num1 + $num2;

echo $res. "<br>";


} else if (isset($_GET['sub'])) {
$num1 = $_GET['first'];
$num2 = $_GET['second'];
$res = $num1 - $num2;

echo $res. "<br>";


} else {
$num1 = $_GET['first'];
$num2 = $_GET['second'];
$res = $num1 * $num2;

echo $res. "<br>";


}
?>
</body>
</html>

You might also like