Qb - Answers ( Php ) Unit - V
Qb - Answers ( Php ) Unit - V
QUESTION BANK
Long Answer Questions ( 10M ):
1. Explain the Procedure to connect MySQL with PHP.
Ans: XAMPP is a popular software package that includes Apache, MYSQL, PHP, and
Perl. To connect MYSQL with PHP using XAMPP.
Install XAMPP:
Open the “ XAMPP Control Panel “ and start the “ Apache and MYSQL
“ modules.
ii.) Access “ phpMyAdmin”:
• Open your web browser and navigate to “ http://localhost/phpMyAdmin/”.
Or “ localhost:1234”.
• This interface allows you to manage MYSQL databases.
NOTE:
“ localhost:1234 “ -> Open ‘phpMyAdmin’ -> Create ‘new’ database ( Give the
name of the database ) -> Create table [ Enter size of the column; how many
columns you required ] -> Select the datatype names of the columns -> insert
Create a PHP file [ e.g. ‘connect_mysql.php’ ] and use the following code to connect MYSQL.
Source Code:
<?php
// Create Connection
// Check Connection
If($conn->connect_error)
// For Example
// $result = $conn->query($sql);
if($conn->query($sql)===TRUE)
else
{
Echo “ error creating table: “.$conn->error;
$conn->close();
?>
Output:
The INSERT INTO statement is used to add new records to a MYSQL table:
Syntax:
Example:
<?php
$servername= "localhost";
$username= "root";
$password= "";
$dbname = "myDB";
// Create connection
// Check connection
if ($conn->connect_error) {
} else {
$conn->close();
?>
Syntax:
Syntax:
Example:
<?php
$servername = "localhost";
$username = "root";
$dbname = "myDB";
// Create connection
// Check connection
if ($conn->connect_error) {
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "s_id: " . $row["s_id"]. " - Name: " . $row["s_name"]. " " . $row["s_age"]. "<br>";
} else {
$conn->close();
?>
Output:
Syntax:
( Or )
Delete from table_name where column_name= data; // to delete particular/specified column
Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which
record or records that should be deleted. If you omit the WHERE clause, all records will be
deleted!
The following examples delete the record with s_id=101 in the "student" table:
<?php
$servername = "localhost";
$username = "root";
$dbname = "myDB";
// Create connection
// Check connection
if ($conn->connect_error) {
}
// sql to delete a record
} else {
$conn->close();
?>
Output:
…………………………………………………………………………………………………
“ localhost:1234 “ -> Open ‘phpMyAdmin’ -> Create ‘new’ database ( Give the
name of the database ) -> Create table [ Enter size of the column; how many
columns you required ] -> Select the datatype names of the columns -> insert
mysqli_num_rows(result);
Parameter Values
Parameter Description
Syntax:
mysqli_error(connection)
( OR )