PHP old questions

 SHORT ANSWER QUESTIONS

2. 2082 Q.No. 11 OR — Write a PHP script to connect to a MySQL database. [5]

<?php

$host = "localhost";

$user = "root";

$pass = "";

$db   = "school";

$conn = mysqli_connect($host, $user, $pass, $db);

if(!$conn){

    die("Connection failed: " . mysqli_connect_error());

}

echo "Database connected successfully";

?>

4. 2081 GIE Set A Q.No. 11 OR — PHP code to add a record (601, Raju, M, 12-06-2002, Kathmandu) into table info_stud of database “School”. [5]

<?php

$conn = mysqli_connect("localhost","root","","School");

$sql = "INSERT INTO info_stud(regno, name, gender, dob, address)

        VALUES (601, 'Raju', 'M', '12-06-2002', 'Kathmandu')";

if(mysqli_query($conn,$sql)){

    echo "Record inserted successfully";

} else {

    echo "Error: " . mysqli_error($conn);

}

?>

6. 2081 GIE Set B Q.No. 11 OR — PHP program to swap two numbers. [5]

<?php

$a = 10;

$b = 20;

$temp = $a;

$a = $b;

$b = $temp;

echo "After swapping: a = $a, b = $b";

?>

8. 2081 Q.No. 11 OR — Purpose of mysqli_connect() and its parameters. [2+3]

Purpose:

mysqli_connect()` is used to establish a connection between PHP and a MySQL database.

Parameters:

mysqli_connect(host, username, password, database)

1. host – Server location (e.g., "localhost")

2. username – MySQL username

3. password – Password of MySQL user

4. database – Database name to connect to

10. 2080 GIE Set A Q.No. 11 OR – Connect MySQL with PHP (example). [5]

<?php

$conn = mysqli_connect("localhost", "root", "", "mydb");

if($conn){

    echo "Connection successful";

} else {

    echo "Connection failed";

}

?>

12. 2080 GIE Set B Q.No. 11 OR – Explain database connection PHP function for MySQL. [5

mysqli_connect() is the function used to connect PHP with MySQL.

It takes four arguments: host, username, password, and database name.

If connection succeeds, it returns a connection object; otherwise, it returns false.

Example:

$conn = mysqli_connect("localhost","root","","testdb");

13. 2080 Q.No. 11 — Syntax + code to insert data into student table. [1+4

Syntax for MySQL connectivity

mysqli_connect(host, username, password, dbname);

 

Server-side code

<?php

$conn = mysqli_connect("localhost","root","","studentDB");

$sql = "INSERT INTO student(firstname, lastname, mark, email)

        VALUES('Ram','Shrestha',85,'ram@gmail.com')";

mysqli_query($conn, $sql);

?>

16. 2079 GIE Set A Q.No. 11 OR — Database connection method in PHP

PHP connects to MySQL using `mysqli_connect()` or PDO.

Example:

$conn = mysqli_connect("localhost","root","","test");

 

18. 2079 Set A Q.No. 11 OR — Fetch data from database and display in form

Example:

<?php

$conn = mysqli_connect("localhost","root","","school");

 

$result = mysqli_query($conn,"SELECT * FROM student");

 

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

    echo "<form>

            Name: <input type='text' value='".$row['name']."'><br>

            Class: <input type='text' value='".$row['class']."'>

          </form><br>";

}

?>

22. What is jQuery? Write its uses.

jQuery:

A fast, lightweight JavaScript library used to simplify HTML DOM manipulation, event handling, animations, and AJAX.

 

Uses:

·         Easy HTML element selection

·         Smooth animations

·         Simplified AJAX requests

·         Cross-browser compatibility

 

23. What is PHP? Explain its uses and advantages.

PHP:

PHP (Hypertext Preprocessor) is a server-side scripting language used to create dynamic webpages.

Uses:

·         Form handling

·         Database interaction

·         Creating dynamic websites

·         Session & cookie management

 

Advantages:

·         Open source

·         Easy to learn

·         Supports many databases

·         Fast and efficient

 

24. Basic PHP syntax + example

<?php

echo "Hello World";

?>

 

25. Different operators used in PHP

 

1. Arithmetic: +, -, *, /, %

2. Relational: ==, !=, >, <, >=, <=

3Logical: &&, ||, !

4. Assignment: =, +=, -=, *=

5. Increment/Decrement: ++, --

6. String operators

 

26. PHP program to display largest among three numbers

<?php

$a = 10; $b = 20; $c = 5;

if($a > $b && $a > $c)

    echo "Largest = $a";

elseif($b > $c)

    echo "Largest = $b";

else

    echo "Largest = $c";

?>

 

LONG ANSWER QUESTION

29. Server-side script to create a database, connect with it, create a table, and insert data

<?php

// Create connection

$conn = mysqli_connect("localhost","root","");

 

// Create database

mysqli_query($conn, "CREATE DATABASE mydb");

 

// Select database

mysqli_select_db($conn, "mydb");

 

// Create table

$table = "CREATE TABLE student(

            id INT AUTO_INCREMENT PRIMARY KEY,

            name VARCHAR(30),

            class INT,

            address VARCHAR(30)

          )";

mysqli_query($conn, $table);

 

// Insert data

$insert = "INSERT INTO student(name, class, address)

           VALUES('Raman', 10, 'Bhaktapur')";

mysqli_query($conn, $insert);

 

echo "Database, table created and data inserted successfully";

?>

 

 

Popular posts from this blog

Computer

Sequential Programs