0% found this document useful (0 votes)
38 views6 pages

PHP Programs

Uploaded by

fandugamer25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

PHP Programs

Uploaded by

fandugamer25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Index.

php

<?php
echo "Welcome to get connected to a database <br>";
/*
Ways to connect to a MySQL Database
1. MySQLi extension
2. PDO
*/
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";

// Create a connection
$conn = mysqli_connect($servername, $username, $password);

// Die if connection was not successful


if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful";
}

?>

Create.php

<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";

// Create a connection
$conn = mysqli_connect($servername, $username, $password);

// Die if connection was not successful


if (!$conn)
die("Connection failed: " . mysqli_connect_error());

else{
echo("Connection was successful <br>");
}

// Check for the database creation success


$sql = "CREATE DATABASE MEDB";
if (mysqli_query($conn, $sql))
echo "Database created successfully";
else
echo "Error creating database: " . mysqli_error($conn);
mysqli_close($conn);
?>

Ctable.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "GPC";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table


$sql = "CREATE TABLE Employees(
employeeID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
joining_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {


echo "Table Employees created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

$conn->close();
?>
Insertrecord.php

<?php

// Connecting to the Database


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

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Die if connection was not successful
if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful<br>";
}

// Variables to be inserted into the table


$name = "EL";
$age = "23";

// Sql query to be executed


$sql = "INSERT INTO `phptrip` (`name`, `age`) VALUES ('$name', '$age')";
$result = mysqli_query($conn, $sql);

// Add a new trip to the Trip table in the database


if($result){
echo "The record has been inserted successfully!<br>";
}
else{
echo "The record was not inserted successfully because of this error --->
". mysqli_error($conn);
}
?>

Display.php

<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";
$database = "CS4SEM";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Die if connection was not successful
if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful<br>";
}

$sql = "SELECT * FROM `phptrip`";


$result = mysqli_query($conn, $sql);

// Find the number of records returned


$num = mysqli_num_rows($result);
echo $num;
echo " records found in the DataBase<br>";
// Display the rows returned by the sql query
if($num> 0){

while($row = mysqli_fetch_assoc($result)){
// echo var_dump($row);
echo $row['NAME'] ." ". $row['AGE'];
echo "<br>";
}

}
?>

Update.php

<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";
$database = "CS4SEM";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Die if connection was not successful


if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful<br>";
}
$sql = "SELECT * FROM `phptrip` WHERE `AGE`='22'";
$result = mysqli_query($conn, $sql);

// Usage of WHERE Clause to fetch data from the database


$num = mysqli_num_rows($result);
echo $num . " records found in the DataBase<br>";
$no=1;
if($num> 0){
while($row = mysqli_fetch_assoc($result)){
echo $no . $row['NAME'] . $row['AGE'];
echo "<br>";
$no = $no +1;
}
}

// Usage of WHERE Clause to Update Data


$sql = "UPDATE `phptrip` SET `NAME` = 'GPC' WHERE `AGE` = '22'";
$result = mysqli_query($conn, $sql);

$aff = mysqli_affected_rows($conn);
echo "<br>Number of affected rows: $aff <br>";
if($result){
echo "We updated the record successfully";
}
else{
echo "We could not update the record successfully";
}
?>

Delete.php

<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";
$database = "CS4SEM";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Die if connection was not successful


if (!$conn){
die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
echo "Connection was successful<br>";
}

$sql = "DELETE FROM `phptrip` WHERE `age` = '26'";


$result = mysqli_query($conn, $sql);
$aff = mysqli_affected_rows($conn);
echo "<br>Number of affected rows: $aff <br>";

if($result){
echo "Delete successfully";
}
else{
$err = mysqli_error($conn);
echo "Not Delete successfully due to this error --> $err";
}

?>

Insert.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "EEDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Add record in table
$sql = "INSERT INTO Employees (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

You might also like