0% found this document useful (0 votes)
4 views

php 3rd

The document provides a comprehensive overview of various MySQL and PHP operations, including data types, delete, insert, update, and database creation operations. It explains how to connect to a MySQL database using PHP, the syntax for SQL queries, and the advantages of using PHP with MySQL. Additionally, it covers the use of MySQLi and PDO for database interactions and includes example code snippets for practical implementation.

Uploaded by

suyashc81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

php 3rd

The document provides a comprehensive overview of various MySQL and PHP operations, including data types, delete, insert, update, and database creation operations. It explains how to connect to a MySQL database using PHP, the syntax for SQL queries, and the advantages of using PHP with MySQL. Additionally, it covers the use of MySQLi and PDO for database interactions and includes example code snippets for practical implementation.

Uploaded by

suyashc81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Datatypes in MYSql
Ans:

2. Explain delete operation of PHP on table data


Ans:
You can delete records from a table using the SQL DELETE statement.
The DELETE statement is usually used with a WHERE clause. This ensures that
only the records matching a specific condition are deleted.
You write the DELETE SQL query with the WHERE clause to define which records
to delete. Then, you pass this query to PHP's query() function to execute and
remove the records.
For example, if you want to delete the student record with the roll number
"CO103", you can use the DELETE statement along with the WHERE clause to delete
that specific record.
3. Inserting and retrieving the query result operations
Ans:
<?php
$conn = new mysqli("localhost", "username", "password", "database_name");

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$query = "INSERT INTO user (roll_number, name) VALUES (1, 'Amit')";


$result = $conn->query($query); // Use the object-oriented method to execute the
query

if ($result) {
echo 'Insertion Successful <br>';
} else {
echo 'Insertion Unsuccessful <br>';
}

$query = "SELECT * FROM user";


$result = $conn->query($query); // Use the object-oriented method to execute the
query

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();
?>

4. How do you connect MySQL database with PHP


Ans:
Using MySQLi Object Interface:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
mysqli_close($conn);
?>

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.

5. Write update operation on table


Ans:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "your_database_name"; // Replace with your database name

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Step 1: Define the UPDATE SQL query


$query = "UPDATE user SET name='Amit Kumar' WHERE roll_number=1"; // Update
name where roll_number is 1

// Step 2: Execute the query


$result = $conn->query($query); // Use the object-oriented approach

// Step 3: Check if the query was successful


if ($result) {
echo "Record updated successfully.";
} else {
echo "Error updating record: " . $conn->error;
}

// Step 4: Close the connection


$conn->close();
?>
6. List database operations
Ans:
1) Create
2) Read
3) Update
4) Delete

7. Write steps to create database using PHP


Ans:

8. Explain queries to update and delete data in the database


Ans:
Update data : UPDATE query
Update command is used to change / update new value for field in
row of table. It updates the value in row that satisfy the criteria given
in query.

The UPDATE query syntax:


UPDATE Table_name SET field_name=New_value WHERE
field_name=existing_value
Example :
UPDATE student SET rollno=4 WHERE name='abc'
In the above query, a value from rollno field from student table is
updated with new value as 4 if its name field contains name as ‘abc’.

Delete data: DELETE query


Delete command is used to delete rows that are no longer required
from the database tables. It deletes the whole row from the table.

The DELETE query syntax:


DELETE FROM table_name WHERE some_column =
some_value

[WHERE condition] is optional. The WHERE clause specifies which


record or records that should be deleted. If the WHERE clause is not
used, all records will be deleted.
Example :-
$sql = "DELETE FROM student WHERE rollno=2";

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";
}
?>

10. Define MYSQL


Ans:
MySQL is used to manage stored data and is an open source Database Management
Software (DBMS) or Relational Database Management System (RDBMS).

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";

$conn = new mysqli($servername, $username, $password);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE DATABASE clg";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>

13. Advantages of php MySql


Ans:
1) It is platform-independent. PHP-based applications can run on any OS like UNIX,
Linux, Windows, etc.
2) It helps in managing code easily.
3) It has powerful library support to use various function modules for data
representation
4) The most important advantage of PHP is that it’s open-source and free from
cost. It can be downloaded anywhere and is readily available to use for events or
web applications.

14. Count total rows


<?php
// Connect to the database
$conn = mysqli_connect("localhost", "root", "", "school");

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Query to count rows in the 'student' table


$query = "SELECT COUNT(*) AS total FROM student";
$result = mysqli_query($conn, $query);

// Fetch and display the count


$row = mysqli_fetch_assoc($result);
echo "Total number of rows: " . $row['total'];

// Close the connection


mysqli_close($conn);
?>

w-24 q.6a)

You might also like