Computer ScienceNEB 2081
Write a JavaScript function that checks if a number is even or odd and print the result. [1+4] OR What is purpose of the mysqli connect ( ) function in PHP ? Describe its usage and parameters. [2+3]
5Answer
JavaScript Function to Check Even or Odd
function checkEvenOdd(number) {
if (number % 2 === 0) {
console.log(`${number} is an even number.`);
} else {
console.log(`${number} is an odd number.`);
}
}
// Example usage:
checkEvenOdd(4); // Output: 4 is an even number.
checkEvenOdd(7); // Output: 7 is an odd number.
Explanation:
- The function
checkEvenOddtakes a single parameternumber. - It checks if the number is divisible by 2 using the modulus operator (
%).- If
number % 2 === 0, the number is even. - Otherwise, it is odd.
- If
- The result is printed using
console.log()with a formatted string.
Purpose and Usage of mysqli_connect() in PHP
Purpose:
The mysqli_connect() function in PHP is used to establish a connection to a MySQL database using the MySQL Improved (mysqli) extension. It allows PHP scripts to interact with MySQL databases by sending queries and retrieving results.
Syntax:
mysqli_connect([string $host = "localhost"], [string $username = ""], [string $password = ""], [string $database = ""] [, int $port = 3306] [, string $socket = ""])
Parameters:
| Parameter | Description | Default Value |
|---|---|---|
$host |
Database server hostname (e.g., "localhost" or "127.0.0.1"). |
"localhost" |
$username |
Database username. | "" (empty) |
$password |
Database password. | "" (empty) |
$database |
Database name (optional). | "" (empty) |
$port |
Database port number (default: 3306). |
3306 |
$socket |
Unix socket path (Linux/Unix systems). | "" (empty) |
Example Usage:
<?php
$conn = mysqli_connect("localhost", "root", "password", "test_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo "Connected successfully!";
mysqli_close($conn); // Close connection when done
}
?>
Key Notes:
- Always check if the connection is successful using
mysqli_connect_error(). - Use
mysqli_close()to free up resources after database operations. - For security, avoid hardcoding credentials; use environment variables or configuration files.
Discussion
Loading…
More Computer Science questions
Which of the following is the purpose of using primary key in database ? a) To uniquely identify a record b) To store duplicate record c) To backup data d) To…NEB 2082 (MCQs)1A company needs to modify an existing table by adding a new column for employee email addresses. For this, which SQL command should be used ? a) CREATE b)…NEB 2082 (MCQs)1Which protocol is used for secure communication over a computer network ? a) HTTP b) FTP c) HTTPS d) TelnetNEB 2082 (MCQs)1Which control structure in JS (Java Script) is used to execute a block of code repeatedly based on a given condition ? a) For loop b) if else c) switch case…NEB 2082 (MCQs)1Select the invalid variable name in PHP. a) $name b) $ name c) $1name d) $name123NEB 2082 (MCQs)1The statement int number ( int a); is a ... a) function call b) function definition c) function declaration d) function executionNEB 2082 (MCQs)1