php 3rd
php 3rd
Datatypes in MYSql
Ans:
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result) {
echo 'Insertion Successful <br>';
} else {
echo 'Insertion Unsuccessful <br>';
}
if ($result->num_rows > 0) {
// Step 4: Fetch and display each record
while ($r = $result->fetch_assoc()) {
echo $r['roll_number'] . ' ' . $r['name'] . '<br>';
}
} else {
echo "No records found.<br>";
}
$conn->close();
?>
The first part of the script is three variables (server name, username, and password)
and their respective values. These values should correspond to your connection
details.
Next is the main PHP function mysqli_connect(). It establishes a connection with
the specified database. When the connection fails, it gives the message Connection
failed. The die function prints the message and then exits out of the script.
If the connection is successful, it displays “Connected successfully.” When the
script ends, the connection with thedatabase also closes. If you want to end the
code manually, use the mysqli_close function.
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
9. Mysqli_connect()
Ans:
1) mysqli_connect() is used to connect PHP to a MySQL database by providing the
server name, username, password, and database name.
2) If the connection is successful, it returns a connection object; otherwise, it
returns false, which should be checked to handle connection errors.
3) This function is part of the MySQLi extension and is mainly used in procedural
programming in PHP.
4) It's important to check the connection using if (!$conn) and display the error
using mysqli_connect_error() if the connection fails.
<?php
$conn = mysqli_connect("localhost", "root", "", "school");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo "Connected successfully";
}
?>
11. POD::__construct()
Ans:
1) PDO::__construct() is called automatically when you create a new PDO object —
it connects your PHP script to the database.
2) It requires a DSN (Data Source Name), along with username and password to
connect.
3) PDO allows prepared statements and exception handling, making it more
secure and professional than mysqli.
4) PDO works with many types of databases (MySQL, PostgreSQL, SQLite, etc.), not
just MySQL.
12. Create
Ans:
CREATE – A database consists of one or more tables. You will need special CREATE
privileges to create or to delete a MySQL database.
A database is a collection of data. MySQL allows us to store and retrieve the data
from the database in a efficient way
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "clg";
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
w-24 q.6a)