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

php_prac_15

The document outlines a practical exercise involving PHP scripts to connect to a MySQL database, create a 'Student' table, insert a record, and display records from the table. It includes code snippets for establishing a database connection, creating a table, inserting data, and retrieving data. Each section provides feedback on the success or failure of the operations performed.

Uploaded by

saeedarwatkar
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)
16 views

php_prac_15

The document outlines a practical exercise involving PHP scripts to connect to a MySQL database, create a 'Student' table, insert a record, and display records from the table. It includes code snippets for establishing a database connection, creating a table, inserting data, and retrieving data. Each section provides feedback on the success or failure of the operations performed.

Uploaded by

saeedarwatkar
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

Practical No.

15
1) Develop an application to create a connection and create a table Student in database.

#Database Connection

<?php

$servername="localhost";

$password="";

$username="root";

$dbname="saee_db";

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

if($res)

echo "Connection established...<br>";

else

echo "Connection not established...<br>";

#create table:

$sql="create table Student(id int(10), first_name varchar(20), email varchar(20))";

if(mysqli_query($res,$sql))

echo "Table created...<br>";

else
{

echo "Table not created...".mysqli_error($res);

mysqli_close($res);

?>

Output:
2) Write a program to insert a record in table.
<?php

$servername="localhost";

$password="";

$username="root";

$dbname="saee_db";

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

if($res)

echo "Connection established...<br>";

else

echo "Connection not established...<br>";

$sql="insert into Student(id,first_name,email)values(101,'saee','[email protected]')";

if(mysqli_query($res,$sql))

echo "Record inserted...<br>";

else

echo "Record not inserted...";

mysqli_close($res);

?>
Output:
3) Write a program to display records.
<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "saee_db";

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

if ($res) {

echo "Connection established...<br>";

} else {

echo "Connection not established...<br>";

$sql = "SELECT id, first_name, email FROM student";

$r = mysqli_query($res, $sql);

if (mysqli_num_rows($r) > 0) {

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

echo $row["id"] . " - " . $row["first_name"] . " - " . $row["email"] . "<br>";

} else {

echo "0 result";


}

mysqli_close($res);

?>

Output:

You might also like