Web Technology 2
Web Technology 2
Syntax:
Parameters:
1. host (Required) – The hostname or IP address of the MySQL server (e.g., "localhost" or
"127.0.0.1").
2. username (Required) – The MySQL database username.
3. password (Required) – The password for the specified username.
4. database (Optional) – The name of the database to connect to.
5. port (Optional) – The port number for the MySQL server (default is 3306).
6. socket (Optional) – The socket or named pipe used for the connection.
Example :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";
// Create connection
// Check connection
if (!$conn) {
?>
2. How do you connect MySQL database with PHP? Explain with an example.
<?php
if (!$conn) {
mysqli_close($conn);
?>
<script>
btn.onclick = function() {
alert("Button Clicked!");
};
</script>
<script>
btn.addEventListener("click", function() {
alert("Button Clicked!");
});
</script>
In PHP, connecting to a MySQL database is essential for building dynamic web applications. The
mysqli_connect() function is commonly used to establish this connection. To make the connection reusable,
a function can be created that takes care of the database connection process. For example, the
connectDatabase() function defines the database credentials such as the server name, username, password,
and database name. It then uses mysqli_connect() to establish the connection and returns the connection
object if successful. If the connection fails, it displays an error message using mysqli_connect_error(). This
function can be included in multiple files to avoid repeating the connection code, making the application
more organized and maintainable. After using the connection, it is a good practice to close it using
mysqli_close($conn) to free up resources. By using a function for database connection, PHP applications
become more scalable and easier to manage.
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$database = "my_database";
// Create connection
// Check connection
if (!$conn) {
// Close connection
mysqli_close($conn);
?>
6. How do you fetch data from database in PHP and display it in form? Describe.
Fetching data from a MySQL database and displaying it in an HTML form allows users to view and edit
existing records. This is commonly used in update/edit forms where users can modify stored data.
<?php
if (!$conn) {
// Step 2: Fetch Data from Database (Assuming user ID is passed via URL)
$id = $_GET['id']; // Get ID from URL
if ($row = mysqli_fetch_assoc($result)) {
?>
<label>Name:</label>
<label>Email:</label>
<label>Phone:</label>
</form>
<?php
} else {
// Close connection
mysqli_close($conn);
?>
Internet technology refers to the various tools, protocols, and systems that enable communication, data
exchange, and online services over the Internet. It plays a crucial role in modern life, supporting web
browsing, email, social media, e-commerce, cloud computing, and more.
Key Components of Internet Technology:
1. Web Technologies – Includes HTML, CSS, JavaScript, and frameworks like React and Angular for building
websites and web applications.
2. Networking Protocols – Protocols like TCP/IP, HTTP, HTTPS, FTP, and DNS enable data transmission and
communication.
3. Internet Services – Services such as email, VoIP (Skype, Zoom), cloud storage (Google Drive, Dropbox), and
streaming (Netflix, YouTube).
4. Wireless & Mobile Internet – Includes Wi-Fi, 4G, 5G, and satellite communication, allowing seamless
connectivity.
5. Cybersecurity – Technologies like firewalls, encryption, and VPNs help protect data from cyber threats.
Internet technology continues to evolve, shaping the digital world with advancements like AI, IoT, and
blockchain.
It does not provide security for data. It provides more security for data.
It is a technique that uses scripts on the
It is a technique used in web development in
webserver to produce a response that is
which scripts run on the client’s browser.
customized for each client’s request.
HTML, CSS, and javascript are used. PHP, Python, Java, Ruby are used.
No need of interaction with the server. It is all about interacting with the servers.
It reduces load on processing unit of the server. It surge the processing load on the server.
8. Explain different data types used in JavaScript.
JavaScript has two main categories of data types: Primitive and Non-Primitive.
jQuery is a fast, lightweight, and feature-rich JavaScript library that simplifies HTML document traversal
and manipulation, event handling, animation, and AJAX interactions. It was designed to make it easier to
work with JavaScript, enabling developers to write less code while achieving more functionality.
Uses of jQuery:
1. DOM Manipulation:
o Easily select and modify HTML elements.
o Add, remove, or update content dynamically.
o Example: $('#element').hide (); hides an element.
2. Event Handling:
o Simplifies handling events like clicks, mouse movements, and keyboard actions.
o Example: $('#button').click (function () {alert ('Clicked!'); });
3. AJAX Requests:
o Facilitates asynchronous requests to fetch data without reloading the page.
Provides built-in methods for creating animations and effects like fading, sliding, and
showing/hiding elements.
Example: $('#element').fadeIn();
5. Cross-browser Support:
Handles inconsistencies between different web browsers, ensuring a smoother user experience.
6. Plugins:
Supports a wide range of plugins to extend its functionality for tasks such as form validation, image
sliders, and more.
Makes it easier to submit forms and handle responses without a full page refresh.
Uses of PHP:
1. Web Development:
o PHP is commonly used to build dynamic websites and web applications. It can handle forms,
sessions, and manage databases.
2. Database Interaction:
o PHP seamlessly integrates with databases like MySQL, PostgreSQL, and others, enabling developers
to create data-driven applications.
3. Content Management Systems (CMS):
o Popular CMS platforms like WordPress, Joomla, and Drupal are built using PHP, allowing users to
create and manage content easily.
4. E-commerce Solutions:
o PHP is used to develop e-commerce platforms, enabling businesses to sell products and services
online (e.g., Magento, WooCommerce).
5. Web APIs:
o PHP can be used to create RESTful APIs, allowing different applications to communicate and
exchange data over the web.
6. Session Management:
o PHP provides built-in support for managing user sessions and cookies, facilitating user
authentication and tracking.
Advantages of PHP:
1. Open Source:
o PHP is free to use and has a large community of developers contributing to its continuous
improvement and support.
2. Cross-Platform Compatibility:
o PHP runs on various operating systems (Windows, Linux, macOS) and can work with different web
servers (Apache, Nginx, IIS).
3. Ease of Learning:
o PHP has a simple and straightforward syntax, making it accessible for beginners and reducing the
learning curve.
4. Wide Community Support:
o A vast community of developers provides extensive documentation, tutorials, and forums, making it
easier to find help and resources.
5. Rich Set of Built-in Functions:
o PHP offers a wide range of built-in functions for various tasks, such as string manipulation, file
handling, and date/time processing.
6. Integration with Databases:
o PHP supports various database systems, making it easy to interact with databases and manage data
efficiently.
7. Scalability and Performance:
o PHP can handle a large number of concurrent users and is capable of scaling web applications to
meet growing demands.
1. Arithmetic Operators
2. Assignment Operators
Simple Assignment (=): Assigns the value of the right operand to the left operand.
o Example: $a = 5;
Addition Assignment (+=): Adds the right operand to the left operand and assigns the result.
o Example: $a += 3; (If $a was 5, it becomes 8.)
Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result.
o Example: $a -= 2; (If $a was 5, it becomes 3.)
Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result.
o Example: $a *= 2; (If $a was 5, it becomes 10.)
Division Assignment (/=): Divides the left operand by the right operand and assigns the result.
o Example: $a /= 5; (If $a was 10, it becomes 2.)
Modulus Assignment (%=): Applies the modulus operator and assigns the result.
o Example: $a %= 3; (If $a was 5, it becomes 2.)
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean result (true or false).
4. Logical Operators
These operators are used to increase or decrease the value of a variable by one.
6. String Operators
7. Array Operators
8. Ternary Operator
10. Develop a program in JavaScript to exchange/swap the values of any two variables
console.log("Before swapping:");
console.log("a =", a);
console.log("b =", b);
Write a JavaScript function that checks if a number is even or odd and print the result.
if (number % 2 === 0) {
console.log(number + " is even.");
} else {
console.log(number + " is odd.");
}
}
// Example usage
checkEvenOdd(10); // Output: 10 is even.
checkEvenOdd(7); // Output: 7 is odd.
checkEvenOdd(0); // Output: 0 is even.
checkEvenOdd(-3); // Output: -3 is odd.
checkEvenOdd("5"); // Output: Please enter a valid number.
11. Write a program to find the largest number among three numbers in JavaScript.
// Function to find the largest number among three numbers
function findLargestNumber(num1, num2, num3) {
if (num1 >= num2 && num1 >= num3) {
return num1; // num1 is the largest
} else if (num2 >= num1 && num2 >= num3) {
return num2; // num2 is the largest
} else {
return num3; // num3 is the largest
}
}
// Example usage
const number1 = 25;
const number2 = 30;
const number3 = 20;
console.log ("The largest number among", number1, number2, "and", number3, "is:", largest);
12. What are the uses of Java Script in web page development?
JavaScript is a powerful programming language widely used in web development for a variety of purposes.
Here are some of its primary uses:
1. Client-Side Scripting: JavaScript is primarily used as a client-side scripting language. It runs in the
user's web browser, allowing for interactive and dynamic content without requiring a page reload.
2. Form Validation: JavaScript can validate user input in forms before submitting data to the server.
This ensures that users enter the correct data format, improving user experience and reducing server
load.
3. Dynamic Content Updates: JavaScript enables dynamic content changes on a webpage without
reloading the entire page. This is often achieved through AJAX (Asynchronous JavaScript and
XML), allowing for smooth user interactions.
4. Event Handling: JavaScript can respond to user actions such as clicks, mouse movements, and
keyboard inputs. This interactivity is crucial for creating engaging user interfaces and improving user
experience.
5. Animations and Effects: JavaScript can create animations, transitions, and other visual effects on
web pages. Libraries like jQuery and frameworks like GSAP enhance these capabilities.
6. Manipulating HTML and CSS: JavaScript can modify the DOM (Document Object Model),
allowing developers to change HTML elements and CSS styles dynamically. This enables real-time
updates and modifications to the webpage.
7. Creating Interactive Games: JavaScript is used in web-based games, allowing developers to create
interactive and engaging experiences directly in the browser.
8. Browser-Based Applications: With frameworks like React, Angular, and Vue.js, JavaScript can be
used to build complex single-page applications (SPAs) that offer a desktop-like experience in a web
browser.
9. Accessing APIs: JavaScript can make HTTP requests to external APIs, allowing web applications to
retrieve and display data from various sources, such as social media, weather services, and more.
10. Cross-Platform Development: JavaScript, along with frameworks like Node.js, enables server-side
development, allowing developers to use the same language for both front-end and back-end
development.
13. Write down the server-side script to create a database, connect with it, create a
table and insert data in it.
<?php
// Database configuration
$host = 'localhost'; // MySQL server host
$username = 'root'; // MySQL username
$password = ''; // MySQL password
$dbname = 'my_database'; // Name of the database
// Create connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully to MySQL server.<br>";
// Create database
$sql = "CREATE DATABASE $dbname";
if ($conn->query($sql) === TRUE) {
echo "Database '$dbname' created successfully.<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}
// Create table
$table_sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";