UNIT 5-part 2
UNIT 5-part 2
3. Prepare
This function prepares an SQL statement for execution. The prepare() function is used to prepare an
SQL statement for execution. It’s useful for preventing SQL injection by separating SQL logic from
data.
$stmt = $connection->prepare($sql);
$stmt = $connection->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
Here, insert query is created as a prepared statement with placeholder ‘?’ which will be later replaced
by using bind_param() beore execution.
4. bind_param()
5. Execute()
This function executes a prepared query. The execute() function is used to execute a prepared
statement.
Syntax:
$stmt->execute();
Example:
$username = "john";
$email = "[email protected]";
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
6. fetch_assoc:
This function fetches a result row as an associative array. The fetch_assoc() function fetches a result
row as an associative array where the keys are the column names.
Syntax:
$row = $result->fetch_assoc();
Example:
while($row = $result->fetch_assoc()) {
echo "Username: " . $row["username"] . " - Email: " . $row["email"] . "<br>";
}
The fetch_assoc() method fetches the next row from the result set as an associative array.
// 2. Check connection
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
// 4. Bind parameters
$username = "john_doe";
$email = "[email protected]";
$stmt->bind_param("ss", $username, $email);
Example:
$stmt = $connection->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
Example Script:
$connection = new mysqli("localhost", "root", "password", "database_name");
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$stmt = $connection->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
$username = "john_doe";
$email = "[email protected]";
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql = "SELECT student_name, register_number FROM students";
$result = $connection->query($sql);
MySQL client:
The MySQL client is a command-line utility designed for interacting with MySQL database
servers. It provides a direct way to connect to the server, execute SQL queries, and manage databases
from the terminal. Users can perform a wide range of tasks, including creating and modifying databases,
querying data, and handling administrative functions such as backups and restores. The client is highly
efficient for these tasks, allowing for quick and precise control over database operations.
While the MySQL client does not offer a graphical user interface, it compensates with powerful
features and flexibility. It supports secure connections via SSL to ensure encrypted communication
between the client and the server. Additionally, the client can execute SQL scripts, making it ideal for
automating repetitive tasks and managing complex operations. Although it may have a steeper learning
curve for those unfamiliar with command-line tools or SQL, it remains an essential tool for developers
and database administrators who require robust database management capabilities.