0% found this document useful (0 votes)
5 views14 pages

Mysql Programs

The document contains multiple PHP applications demonstrating how to interact with a MySQL database using mysqli. It includes code for creating a database, creating a table, inserting rows, fetching rows, deleting a table, and handling user input through a form. Each section provides the PHP code necessary for the respective database operation along with connection handling and error checking.

Uploaded by

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

Mysql Programs

The document contains multiple PHP applications demonstrating how to interact with a MySQL database using mysqli. It includes code for creating a database, creating a table, inserting rows, fetching rows, deleting a table, and handling user input through a form. Each section provides the PHP code necessary for the respective database operation along with connection handling and error checking.

Uploaded by

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

1. Write a PHP application to create database in mysqli.

<?php

echo “<body bgcolor=pink>”;

$servername = "127.0.0.1";

$username = "phpwithmysql1";

$password = "123456";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error)

die("Connection failed: " . $conn->connect_error);

echo "Connected successfully<br>";

// Create database

$sql = "CREATE DATABASE CSE";

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

echo "Database created successfully";

} else {

echo "Error creating database: " . $conn->error;

$conn->close();

?>
Output:
2.Write a PHP application to create table.

<?php

echo "<body bgcolor=yellow>";

$servername = "127.0.0.1";

$username = "phpwithmysql";

$password = "123456";

$dbname = "cse";

// 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 emp(

eno int(3),ename varchar(15),sal int(8),dno int(3)

)";

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

echo "Table emp created successfully<br>";

} else {

echo "Error creating table: " . $conn->error;

$conn->close();
?>

3. Write a PHP application to insert Rows into a Table.

<?php

echo "<body bgcolor=yellow>";

$servername = "127.0.0.1";

$username = "phpwithmysql";

$password = "123456";

$dbname = "cse";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO emp (eno,ename,sal,dno)

VALUES (102,'kavitha',20000,202)";

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

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();

?>
Output:
4. Write a PHP application to fetch the Rows from the table.

<?php

echo "<body bgcolor=orange>";

$servername = "127.0.0.1";

$username = "phpwithmysql";

$password = "123456";

$dbname = "cse";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "SELECT eno,ename,sal,dno from emp";

$result = $conn->query($sql);

if ($result->num_rows> 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "Eno: " . $row["eno"]."<br>".

" Ename: " . $row["ename"]. "<br>".

"Salary " . $row["sal"]."<br>".

"Dno " . $row["dno"]."<br>";

} else {
echo "0 results";

$conn->close();

?>

5. Write a PHP application to delete the table.

<?php

echo "<body bgcolor=orange>";

$servername = "127.0.0.1";

$username = "phpwithmysql";

$password = "123456";

$dbname = "cse";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "delete from emp";

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

echo "Table emp deleted successfully<br>";

} else {

echo "Error creating table: " . $conn->error;

}
$conn->close();

?>
Output:
6.Write a program to read user information like name, email from
user and display all these information on output screen.

form.html

<html>

<body bgcolor="pink">

<form action="welcome.php" method="post">

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

<input type="submit">

</form>

</body>

</html>
welcome.php

<?php

echo "<body bgcolor=yellow>";

$name=$_POST["name"];

$email=$_POST["email"];

$conn = new mysqli("127.0.0.1","phpwithmysql","123456","cse");

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO Login_details values('$name','$email')";

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

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

echo "your details are<br>";

$sql = "SELECT name,email from Login_details";

$result = $conn->query($sql);

if ($result->num_rows> 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "name " . $row["name"]."<br>".

" Email: " . $row["email"]. "<br>";


}

} else {

echo "0 results";

$conn->close();

?>
Output:

You might also like