Chapter 5 PHP
Chapter 5 PHP
1. Define MySQL.
Answer:
MySQL is used to manage stored data and is an open source Database Management Software
(DBMS) or Relational Database Management System (RDBMS).
2. POD::_construct()
Answer:
POD::_construct() in PHP
2 marks
The POD::__construct() function in PHP is used to create a new instance of the
PDO (PHP Data Objects) class. PDO provides a consistent way to interact with
different database management systems (DBMS) like MySQL, PostgreSQL, Sqlite
etc.
Here are the key parameters for the PDO::__construct() function:
1. DSN (Data Source Name) (1 mark)
o This is a required parameter that specifies the connection information for the
database. The format of the DSN string varies depending on the specific DBMS you
are connecting to. For example, a MySQL DSN might look like
mysql:host=localhost;dbname=mydatabase.
2. Username (1 mark)
o This is an optional parameter that specifies the username for the database
connection.
3. Password (1 mark)
o This is an optional parameter that specifies the password for the database
connection.
4. Options (1 mark)
o This is an optional parameter that is an associative array of driver-specific
connection options. These options can be used to configure things like persistent
connections or error handling.
Example
PHP
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $username, $password);
// PDO connection established successfully
echo 'Connected to database';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Additional Points
It's important to remember to escape any user input used in the DSN or when
constructing queries to prevent SQL injection attacks.
PDO provides a more secure and object-oriented way to interact with databases
compared to older functions like mysql_connect.
3. mysqli_connect()
Answer:
mysqli_connect() in PHP
2 Marks
The mysqli_connect() function in PHP is employed to establish a connection to a
MySQL database server. It returns a connection object on success, and FALSE if the
connection fails.
Parameters (1 Mark)
The mysqli_connect() function accepts up to six parameters as shown in the
image:
1. host (Optional):
o This parameter specifies the hostname or IP address of the MySQL server. By
default, it assumes localhost, which is the server where your PHP script is running.
2. username (Optional):
o This parameter represents the username for MySQL authentication. If omitted, it
defaults to the value of the PHP_USER environment variable.
3. password (Optional):
o This parameter represents the password for MySQL authentication. If omitted, it
defaults to the value of the PHP_PASSWORD environment variable.
4. dbname (Optional):
o This parameter specifies the name of the database you want to connect to. If
omitted, the default database associated with the username is used.
5. port (Optional):
o This parameter specifies the port number on the MySQL server to connect to. The
default port for MySQL is 3306.
6. socket (Optional):
o This parameter specifies the socket or named pipe to be used for the connection.
This is typically only used on Unix-like systems for local connections.
Example:
PHP
<?php
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$dbname = 'mydatabase';
?>
Important Note:
Specifying username and password directly in the script is not recommended for
security reasons. Consider using environment variables or a configuration file to
store sensitive credentials.
Answer:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "test_db";
// Create connection
// Check connection
if ($conn->connect_error) {
// Data to be inserted
$email = "[email protected]";
} else {
$stmt->close();
$conn->close();
?>
Answer:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "test_db";
// Create connection
// Check connection
if ($conn->connect_error) {
if ($result->num_rows > 0) {
echo "<tr>";
echo "</tr>";
echo "</table>";
} else {
$conn->close();
?>
Answer:
7. Write update and delete operations on table data.
Answer: