student table display records
<?php
//EstablishconnectiontoPostgreSQLdatabase
$conn=pg_connect(“host=localhostdbname=your_database_nameuser=your_username
password=your_password”);
//Checkifconnectionwassuccessful
If(!$conn){
Echo“Connectionfailed.”;
Exit;
}
//Createstudenttable
$query=“CREATETABLEstudent(
RollnoINTEGERPRIMARYKEY,
NameVARCHAR(50)NOTNULL,
ClassVARCHAR(10)NOTNULL
)”;
$result=pg_query($conn,$query);
If(!$result){
Echo“Errorcreatingtable:“.pg_last_error($conn);
Exit;
}else{
Echo“Tablecreatedsuccessfully.<br>”;
}
//Insert5recordsintostudenttable
$insert_query=“INSERTINTOstudent(rollno,name,class)
VALUES(1,‘JohnDoe’,‘10A’),
(2,‘JaneSmith’,‘9B’),
(3,‘BobJohnson’,‘11C’),
(4,‘SarahLee’,‘12D’),
(5,‘TomBrown’,‘8E’)”;
$insert_result=pg_query($conn,$insert_query);
If(!$insert_result){
Echo“Errorinsertingrecords:“.pg_last_error($conn);
Exit;
}else{
Echo“Recordsinsertedsuccessfully.”;
}
//Closedatabaseconnection
Pg_close($conn);
//functiontodisplaydatabaserecords
Functiondisplay_records($table_name){
//establishconnectiontoPostgreSQLdatabase
$conn=pg_connect(“host=localhostdbname=your_database_nameuser=your_username
password=your_password”);
//checkifconnectionwassuccessful
If(!$conn){
Echo“Connectionfailed.”;
Exit;
}
//retrieverecordsfromspecifiedtable
$query=“SELECT*FROM“.$table_name;
$result=pg_query($conn,$query);
//checkifquerywassuccessful
If(!$result){
Echo“Errorretrievingrecords:“.pg_last_error($conn);
Exit;
}
//displayrecordsinanHTMLtable
Echo“<table>”;
Echo“<tr><th>RollNo</th><th>Name</th><th>Class</th></tr>”;
While($row=pg_fetch_assoc($result)){
Echo“<tr><td>”.$row[‘rollno’].“</td><td>”.$row[‘name’].“</td><td>”.$row[‘class’].
“</td></tr>”;
}
Echo“</table>”;
//closedatabaseconnection
Pg_close($conn);
}
?