page1.php <html> <head> <title>Connecting MySQLi Server</title> </head> <body> <
ID: 3903926 • Letter: P
Question
page1.php
<html>
<head>
<title>Connecting MySQLi Server</title>
</head>
<body>
<?php
//-------connect to MySQL database system------------------------------------
$dbname="zed2";
$tbname= "instructorInfo";
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
print "Could not connect: " . mysqli_error();
}
print "Connected successfully";
//-------choose a database to use------------------------------------
mysqli_select_db($conn, $dbname);
//-------insert two records into a table------------------------------------
$sql = "INSERT INTO $tbname "."(I_ID,name, phone, department, college )".
"VALUES(1000,"Tammy Connally", "3601234567", "cs", "science")";
$retval = mysqli_query($conn, $sql);
if ($retval)
{
print "data is inserted successfully <br>";
}
else
{
print "Error inserting data to a table: " . mysqli_error($conn);
}
$sql = "INSERT INTO $tbname "."(I_ID,name, phone, department, college )".
"VALUES(1001,"Long Grisham", "36023456789", "English", "arts")";
$retval = mysqli_query($conn, $sql);
if ($retval)
{
print "data is inserted successfully <br>";
}
else
{
print "Error inserting data to a table: " . mysqli_error($conn);
}
$sql = "INSERT INTO $tbname "."(I_ID,name, phone, department, college )".
"VALUES(1002,"Nakisha Voth", "3607654321", "cs", "science")";
$retval = mysqli_query($conn, $sql);
if ($retval)
{
print "data is inserted successfully <br>";
}
else
{
print "Error inserting data to a table: " . mysqli_error($conn);
}
$sql = "INSERT INTO $tbname "."(I_ID,name, phone, department, college )".
"VALUES(1003,"Barton Bembry", "3601230000", "math", "science")";
$retval = mysqli_query($conn, $sql);
if ($retval)
{
print "data is inserted successfully <br>";
}
else
{
print "Error inserting data to a table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
/// retrieve.php
Create a script (retrieve.php) that performs the following operations:
- retrieving the data of all instructors from the database.
- using a for loop to print the data of each instructor.
- when printing the major of each instructor:
- if the retrieved major of an instructor is cs, print the major as computer science;
- if the retrieved major is math, print the major as mathematics;
- if the retrieved major is phy, print the major as physics;
- otherwise, directly print the retrieved major.
Explanation / Answer
<?php
//-------connect to MySQL database system------------------------------------
$dbname="sam";
$tbname= "instructorInfo";
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
print "Could not connect: " . mysqli_error();
}
print "Connected successfully";
//-------choose a database to use------------------------------------
mysqli_select_db($conn, $dbname);
$sql = "SELECT * FROM instructorInfo";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Phone</th>";
echo "<th>Department</th>";
echo "<th>College</th>";
echo "</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['I_ID'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
if($row['department']=='cs'){
echo "<td>" .'Computer Sciene'. "</td>";
} else if($row['department']=='math'){
echo "<td>" .'Mathematics'. "</td>";
}
//echo "<td>" . $row['department'] . "</td>";
echo "<td>" . $row['college'] . "</td>";
}
}else {
echo "0 results";
}
?>
</tr>
</table>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.