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

Assignment 5 WT

The document provides PHP code examples for MySQL connectivity, data insertion, and data retrieval. It includes scripts for establishing a database connection, creating a form for user input, inserting data into a database, and displaying user data in an HTML table. The examples demonstrate basic CRUD operations using PHP and MySQL.

Uploaded by

deshmukhomraje4
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)
2 views

Assignment 5 WT

The document provides PHP code examples for MySQL connectivity, data insertion, and data retrieval. It includes scripts for establishing a database connection, creating a form for user input, inserting data into a database, and displaying user data in an HTML table. The examples demonstrate basic CRUD operations using PHP and MySQL.

Uploaded by

deshmukhomraje4
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/ 9

Assignment No.

5
Q2. Write a code to perform MySQL connectivity with PHP.

Ans- db_connnection.php

<?php function
OpenCon()

{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "1234";
$db = "example";

$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error); return
$conn;

function CloseCon($conn)
{
$conn -> close();
}
?> index.php
<?php

include 'db_connection.php';
$conn = OpenCon(); echo
"Connected Successfully";

CloseCon($conn);
?>

Output:

Q3. Write a PHP code to add data in database.

Ans- index.php

<!DOCTYPE html>
<html lang="en"> <head>

<title>GFG- Store Data</title>


</head> <body>

<center>
<h1>Storing Form data in Database</h1>
<form action="insert.php" method="post">

<p>

<label for="firstName">First Name:</label>


<input type="text" name="first_name" id="firstName">
</p>

<p>

<label for="lastName">Last Name:</label>


<input type="text" name="last_name" id="lastName">
</p>

<p>

<label for="Gender">Gender:</label>
<input type="text" name="gender" id="Gender">
</p>

<p>
<label for="Address">Address:</label>
<input type="text" name="address" id="Address">
</p>

<p>

<label for="emailAddress">Email Address:</label>


<input type="text" name="email" id="emailAddress">
</p>

<input type="submit" value="Submit">


</form>
</center>
</body>
</html>

insert.php
<!DOCTYPE html>
<html>

<head>
<title>Insert Page page</title>
</head>

<body>

<center>
<?php

// servername => localhost


// username => root
// password => empty
// database name => staff
$conn = mysqli_connect("localhost", "root", "", "staff");

// Check connection if($conn


=== false){ die("ERROR:
Could not connect. "

. mysqli_connect_error());
}

// Taking all 5 values from the form data(input)


$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$gender = $_REQUEST['gender'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];

// Performing insert query execution


// here our table name is college
$sql = "INSERT INTO college VALUES ('$first_name',
'$last_name','$gender','$address','$email')";

if(mysqli_query($conn, $sql)){ echo "<h3>data stored in a


database successfully."

. " Please browse your localhost php my admin"


. " to view the updated data</h3>";

echo nl2br("\n$first_name\n $last_name\n "

. "$gender\n $address\n $email");


} else{

echo "ERROR: Hush! Sorry $sql. "


. mysqli_error($conn);

// Close connection
mysqli_close($conn);

?>
</center>
</body>

</html>
Output:
Q5. Write a PHP code to retrieve results from Database.

Ans-.
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML and PHP code</title>
</head>
<body>
<h1>Display user list using HTML and PHP</h1>
<?=$msg;?>
<table border="1px" style="width:600px; line-height:40px;">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Status</th>
<th>Registrating Date</th>
</tr>
</thead>
<tbody>
<?php while($row =
mysqli_fetch_assoc($result)){?>

<tr>
<td><?php echo $row['user_firstName'].$row['user_lastName']; ?></td>
<td><?php echo $row['user_email']; ?></td>
<td><?php if($row['user_status'] == 1){
echo "Active";
}else{ echo
"Deactive";

} ?></td>
<td><?php echo $row['user_registrationDate']; ?></td>
<tr>

<?}?>
</tbody>
</table>
</body>
</html>

main.php
<?php
$host = "127.0.0.1"; //IP of your database
$userName = "root"; //Username for database login
$userPass = ""; //Password associated with the username
$database = "example-database"; //Your database name

$connectQuery = mysqli_connect($host,$userName,$userPass,$database);

if(mysqli_connect_errno()){
echo mysqli_connect_error(); exit();

}else{
$selectQuery = "SELECT * FROM `tbl_users` ORDER BY `user_id` ASC";
$result = mysqli_query($connectQuery,$selectQuery);
if(mysqli_num_rows($result) > 0){

}else{
$msg = "No Record found";
}
}
?>
Output-

You might also like