Expected Question and Their Answers by Ritesh of php
Expected Question and Their Answers by Ritesh of php
of php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Q.8) Enlist the attributes of cookies.
Ans. Attributes of Cookies are as follows: List any four
1. name attributes
2. value each
3. expire
4. path
5. domain
6. secure
Q.9) Define session and cookies
Ans:
Cookies
Cookies are small pieces of data stored on the user's
browser by the web server. They help remember information
across different visits to the same website.
Session
A session is a way to store data on the server for individual
users. It helps track users and their data while they navigate
across multiple pages of a website.
<?
session_start();
$_SESSION["username"] = "ritesh";
echo $_SESSION["username"];
?>
Q.10) List types of MYSQL storage engine
Ans: 1)MYISAM
2)InnoDB
3)MEMORY
4)MERGE
5)EXAMPLE
6)ARCHIEVE
Ex:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "your_database_name";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
$email = $_POST['email'];
$conn->close();
?>
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
echo "<table border='1'>
<tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No data found.";
}
$conn->close();
?>
<label for="address">Address:</label><br>
<textarea name="address" rows="4" cols="30"
required></textarea><br><br>
Php form
<?php
echo "<h2>Customer Details Submitted</h2>";
$name = $_POST['name'];
$address = $_POST['address'];
$mobile = $_POST['mobile'];
$dob = $_POST['dob'];
if($_SERVER["REQUEST_METHOD"]=="POST") {
if(empty($_POST["name"])) {
$nerror = "Name cannot be empty!";
}
else {
$name = test_input($_POST["name"]);
if(!preg_match("/^[a-zA-Z-']*$/",$name))
{
$nerror = "Only characters and white spaces allowed";
}
}
if(empty($_POST["email"])) {
$merror = "Email cannot be empty!";
}
else
{
$email = test_input($_POST["email"]);
if(!preg_match($pattern, $email)) {
$merror = "Email is not valid";
}
}
if(empty($_POST["phone"])) {
$perror = "Phone no cannot be empty!";
}
else {
$phone = test_input($_POST["phone"]);
if (!preg_match ('/^[0-9]{10}+$/', $phone)) {
$perror = "Phn no is not valid";
}
}
if(empty($_POST["website"])) {
$werror = "This field cannot be empty!";
}
else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$werror = "URL is not valid";
}
}
if (empty($_POST["comment"])) {
$cerror = "";
}
else {
$comment = test_input($_POST["comment"]);
}}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p><span class="error">* required field </span></p>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo
$nerror;?></span><br/><br/>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo
$merror;?></span><br/><br/>
Phone no: <input type="text" name="phone">
<span class="error">* <?php echo
$perror;?></span><br/><br/>
Website: <input type="text" name="website">
<span class="error">* <?php echo
$werror;?></span><br/><br/>
Comment: <textarea name="comment" rows="5"
cols="40"></textarea><br/><br/>
<input type="submit" name="submit"
value="Submit"></form>
<?php
echo "<h2>Your Input:</h2>";
echo $name; echo "<br>";
echo $email; echo "<br>";
echo $phone; echo "<br>";
echo $website; echo "<br>";
echo $comment;
?>
</body>
</html>
Q.10) How do you connect MYSQL database with PHP
Ans:
Using MySQLi Object Interface:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
mysqli_close($conn);
?>
Explanation:
The first part of the script is three variables (server name,
username, and password) and their respective values. These
values
should correspond to your connection details. Next is the
main PHP function mysqli_connect(). It establishes a
connection with the specified database.
When the connection fails, it gives the message Connection
failed.
The die function prints the message and then exits out of the
script
If the connection is successful, it displays “Connected
successfully.” When the script ends, the connection with the
database also closes. If you want to end the code manually,
use
the mysqli_close function.
if ($conn->connect_error) die($conn->connect_error);
$query = "DELETE from student WHERE rollno='CO103'";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
?>
Q.12) Explain web server role in web development
Ans. The web server's role in PHP web development as
shown below: