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

Qb - Answers ( Php ) Unit - V

The document provides a comprehensive guide on connecting MySQL with PHP using XAMPP, detailing the installation process, starting services, and accessing phpMyAdmin. It includes PHP code examples for creating a database connection, inserting, retrieving, and deleting data from a MySQL table. Additionally, it addresses short answer questions related to MySQL functions in PHP.

Uploaded by

Divya Gurram
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)
20 views

Qb - Answers ( Php ) Unit - V

The document provides a comprehensive guide on connecting MySQL with PHP using XAMPP, detailing the installation process, starting services, and accessing phpMyAdmin. It includes PHP code examples for creating a database connection, inserting, retrieving, and deleting data from a MySQL table. Additionally, it addresses short answer questions related to MySQL functions in PHP.

Uploaded by

Divya Gurram
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/ 10

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.

Follow these steps:

Install XAMPP:

i.) Download XAMPP:

Get the latest version of xampp from the official website.

ii.) Install XAMPP:


Follow the installation instructions provided for your operating systems
( Windows, macOS, Linux ).

Start XAMPP and MYSQL:

i.) Start 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:

XAMPP -> Open Apache, MYSQL -> in ‘ Google Chrome ‘ Enter

“ 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

values of the given columns -> Enter “ go “.


PHP Connection Code:

Create a PHP file [ e.g. ‘connect_mysql.php’ ] and use the following code to connect MYSQL.

Source Code:

<?php

$servername = “localhost”; // Server name

$username = “root”; // Default username for XAMPP MYSQL

$password = “ “; // Default password for XAMPP MYSQL ( Empty by default )

$dbname = “myDB”; // Replace with your database name

// Create Connection

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

// Check Connection

If($conn->connect_error)

die( “Connection failed: “.$conn->connect_error );

echo “Connected Successfully”;

// Perform database operations or queries here

// For Example

// $sql = “ SELECT *from your_table_name “;

// $result = $conn->query($sql);

$sql = “create table student( s_id int(10), s_name varchar(20), s_age(5))”;

if($conn->query($sql)===TRUE)

echo “ Student table created successfully”;

else

{
Echo “ error creating table: “.$conn->error;

$conn->close();

?>

Output:

Student table created successfully

Working with MYSQL Data:


a.) Inserting Data with PHP
After a database and a table have been created, we can start adding data in them.
Here are some syntax rules to follow:
• The SQL Query must be quoted in PHP.
• String values inside the SQL query must be quoted.
• Numeric values must not be quoted.
• The word NULL must not be quoted.

The INSERT INTO statement is used to add new records to a MYSQL table:

Syntax:

INSERT INTO table_name (column1, column2, …….)VALUES (value1, value2,……);

Example:

<?php

$servername= "localhost";
$username= "root";
$password= "";
$dbname = "myDB";

// Create connection

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

// Check connection
if ($conn->connect_error) {

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

$sql = "INSERT INTO student(s_id, s_name, s_age)

VALUES (100, 'Naga', 23)";

if ($conn->query($sql) === TRUE) {

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();
?>

b.) Retrieving Data with PHP:


The SELECT statement is used to select data from one or more tables:

Syntax:

SELECT column_name(s) FROM table_name;

Or we can use the * character to select ALL columns from a table:

Syntax:

SELECT * FROM table_name;

Example:

<?php

$servername = "localhost";
$username = "root";

$password = " ";

$dbname = "myDB";

// Create connection

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

// Check connection

if ($conn->connect_error) {

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

$sql = "SELECT s_id,s_name, s_age FROM student";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "s_id: " . $row["s_id"]. " - Name: " . $row["s_name"]. " " . $row["s_age"]. "<br>";

} else {

echo "0 results";

$conn->close();

?>

Output:

S_id S_name S_age


100 Naga 23
101 Rani 24
102 Radha 22
c) Deleting Data from MYSQL:
The DELETE statement is used to delete records from a table:

Syntax:

Delete from table_name; // delete entire table data

( 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!

Let's look at the " student " table:

S_id S_name S_age


100 Naga 23
101 Rani 24
102 Radha 22
Example:

The following examples delete the record with s_id=101 in the "student" table:

<?php

$servername = "localhost";

$username = "root";

$password = " ";

$dbname = "myDB";

// Create connection

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

// Check connection

if ($conn->connect_error) {

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

}
// sql to delete a record

$sql = "DELETE FROM student WHERE s_id=101";

if ($conn->query($sql) === TRUE) {

echo "Record deleted successfully";

} else {

echo "Error deleting record: " . $conn->error;

$conn->close();
?>

Output:

Record deleted successfully

…………………………………………………………………………………………………

Very Short Answer Questions(2M)


1. How will you connect to a MySQL database using PHP?
Ans: XAMPP -> Open Apache, MYSQL -> in ‘ Google Chrome ‘ Enter

“ 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

values of the given columns -> Enter “ go “.

2. Which PHP Functions counts the number of records in a


resultset.
Ans: In PHP, to count the number of records in a result set, you can use the
mysqli_num_rows() function if you are using MySQLi (MySQL Improved) extension to
interact with a MySQL database. This function returns the number of rows in a result set
obtained from a query.
Syntax:

mysqli_num_rows(result);

Parameter Values

Parameter Description

Result Required. Specifies a result set identifier


returned by mysqli_query(),
mysqli_store_result() or mysqli_use_result()

3. What function retrieves the text of a MYSQL error message.


Ans: In MySQL, you can retrieve the text of a MySQL error message using the
mysqli_error() function in PHP. This function returns a string description of the last error that
occurred during the MySQL connection.

Syntax:

mysqli_error(connection)

( OR )

$mysqli -> error

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy