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

Server Side Lab Manual Using PHP

The document contains 6 PHP programs that demonstrate basic operations for working with databases: 1) Creating a database connection and database, 2) Creating a table in the database, 3) Inserting records into the table, 4) Deleting a record from the table, 5) Updating a record in the table, and 6) Selecting and displaying records from the table. Each program connects to a MySQL database, performs the operation (create, insert, delete, etc.), and then closes the connection.

Uploaded by

N LinkinPark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Server Side Lab Manual Using PHP

The document contains 6 PHP programs that demonstrate basic operations for working with databases: 1) Creating a database connection and database, 2) Creating a table in the database, 3) Inserting records into the table, 4) Deleting a record from the table, 5) Updating a record in the table, and 6) Selecting and displaying records from the table. Each program connects to a MySQL database, performs the operation (create, insert, delete, etc.), and then closes the connection.

Uploaded by

N LinkinPark
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Program demonstrating Database creation and connection to server.

<?php

$servername = "localhost";

$username = "root";

$password = "";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

// Create database

$sql = "CREATE DATABASE BCA";

if (mysqli_query($conn, $sql)) {

echo "Database created successfully";

} else {

echo "Error creating database: " . mysqli_error($conn);

mysqli_close($conn);

?>
2. Program demonstrating Table creation in a database.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "BCA";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

// sql to create table

$sql = "CREATE TABLE STUDENT(

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

firstname VARCHAR(30) NOT NULL,

lastname VARCHAR(30) NOT NULL,

email VARCHAR(50))";

if (mysqli_query($conn, $sql)) {

echo "Table MyGuests created successfully";

} else {

echo "Error creating table: " . mysqli_error($conn);

mysqli_close($conn);

?>
3. Program demonstrating Insert values in a table of a database.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "BCA";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

$sql = "INSERT INTO STUDENT (firstname, lastname, email)

VALUES ('John', 'Doe', '[email protected]');";

$sql .= "INSERT INTO STUDENT (firstname, lastname, email)

VALUES ('Mary', 'Moe', '[email protected]');";

$sql .= "INSERT INTO STUDENT (firstname, lastname, email)

VALUES ('Julie', 'Dooley', '[email protected]')";

if (mysqli_multi_query($conn, $sql)) {

echo "New records created successfully";

} else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);

mysqli_close($conn);

?>
4. Program demonstrating Delete value from a table of a database.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "BCA";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

// sql to delete a record

$sql = "DELETE FROM STUDENT WHERE id=6";

if (mysqli_query($conn, $sql)) {

echo "Record deleted successfully";

} else {

echo "Error deleting record: " . mysqli_error($conn);

mysqli_close($conn);

?>
5. Program demonstrating Update values of table from a database.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "BCA";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

$sql = "UPDATE `student` SET `lastname`= 'Cena' WHERE id=8";

if (mysqli_query($conn, $sql)) {

echo "Record updated successfully";

} else {

echo "Error updating record: " . mysqli_error($conn);

mysqli_close($conn);

?>
6. Program demonstrating Select (Display) some values from a database
table.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "BCA";

// Create connection

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

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

$sql = "SELECT id, firstname, lastname, email FROM STUDENT";

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

if (mysqli_num_rows($result) > 0) {

// output data of each row

while($row = mysqli_fetch_assoc($result)) {

echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>" .
$row["email"]. "<br>";

} else {

echo "0 results";

mysqli_close($conn);

?>

You might also like