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

unit5 web.

This document provides an overview of using MySQL databases in PHP, covering how to connect to a database, perform queries, retrieve and display results, modify data, and delete records. It includes examples of using functions like mysqli_connect(), mysqli_query(), and mysqli_fetch_array() for database operations. Additionally, it demonstrates how to update and delete records in a MySQL table using SQL statements.

Uploaded by

mukilap6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

unit5 web.

This document provides an overview of using MySQL databases in PHP, covering how to connect to a database, perform queries, retrieve and display results, modify data, and delete records. It includes examples of using functions like mysqli_connect(), mysqli_query(), and mysqli_fetch_array() for database operations. Additionally, it demonstrates how to update and delete records in a MySQL table using SQL statements.

Uploaded by

mukilap6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT –V MYSQL DATABASES IN PHP

Introduction – connecting to a MYSQL database – querying the database – retrieving and


displaying the results – modifying data – deleting data. Designing simple applications.

CONNECTING TO A MYSQL DATABASE

• To access data in the MySQL database, we need to be able to connect to the


server:
• Use the function mysqli_connect() to connect to a database and then close the link
when you are finished with it.

mysqli_connect()
• mysqli_connect() gives access to any databases that are assigned to the user.
• The mysqli_connect() function opens a new connection to the MySQL server.
• If you give an invalid login set, it would generate an error, and the exception
handling would allow the application to die gracefully.
Syntax
mysqli_connect(host,username,password,dbname,port,socket);

Parameter Description

host Optional. Specifies a host name or an IP address

username Optional. Specifies the MySQL username

password Optional. Specifies the MySQL password

dbname Optional. Specifies the default database to be used

port Optional. Specifies the port number to attempt to connect to the MySQL server

socket Optional. Specifies the socket or named pipe to be used

Example
<?php
$host = "localhost";
$username = "";
$password = "";
$dbname = "sform";
// Create connection
$conn = mysqli_connect($host, $username, $password, $dbname);

// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
echo "Connection Succees....";
mysqli_close($conn);
?>

Mysqli_close()

The mysqli_close() function closes a previously opened database connection.

Syntax
mysqli_close(connection);

Parameter Description

connection Required. Specifies the MySQL connection to close

Example
<?php
$conn = mysqli_connect($host, $username, $password, $dbname);

// ....some PHP code...

mysqli_close($con);
?>
QUERYING THE DATABASE

➢ SQL allows to perform common functionality such as


• insert, which allows you to enter data into a row;
• alter, which allows you to change the format of a table;
• select, which allows you to return a row set from a table in the database;
• delete, which allows you to remove a row in the database

➢ To perform a query in PHP, WE can use the function mysqli_query().

(i) PHP mysqli_query() Function

The mysqli_query() function performs a query against the database.

Syntax
mysqli_query(connection,query,resultmode);

Parameter Description

connection Required. Specifies the MySQL connection to use

query Required. Specifies the query string

Optional. A constant. Either:


resultmode
• MYSQLI_USE_RESULT (Use this if we have to retrieve large
amount of data)
• MYSQLI_STORE_RESULT (This is default)

Example
<?php
$conn = mysqli_connect($host, $username, $password, $dbname);

// Perform queries
mysqli_query($con,"SELECT * FROM sform");
mysqli_query($con,"INSERT INTO sform (name,fname,mname,dob,mno)
VALUES ('rishi','ritvik',’s’,12-oct-2006)");

mysqli_close($con);
?>
(ii) PHP mysqli_select_db() Function

The mysqli_select_db() function is used to change the default database for the
connection.

Syntax
mysqli_select_db(connection,dbname);

Parameter Description

connection Required. Specifies the MySQL connection to use

dbname Required. Specifies the default database to be used

Example
<?php
$conn = mysqli_connect($host, $username, $password, $dbname);
// Check connection

// ...some PHP code for database "sform"...

// Change database to "test"


mysqli_select_db($conn,"test");

// ...some PHP code for database "test"...

mysqli_close($conn);
?>
RETRIEVING AND DISPLAYING RESULTS

In PHP, the most common method to retrieve a row in the database is with the
mysql_fetch_array() function.

PHP mysqli_fetch_array() Function


The mysqli_fetch_array() function fetches a result row as an associative array, a
numeric array, or both.
Syntax
mysqli_fetch_array(result,resulttype);

Parameter Description

result Required. Specifies a result set identifier returned by mysqli_query(),


mysqli_store_result() or mysqli_use_result()

resulttype Optional. Specifies what type of array that should be produced. Can be
one of the following values:

• MYSQLI_ASSOC
• MYSQLI_NUM
• MYSQLI_BOTH

<?php
$conn = mysqli_connect($host, $username, $password, $dbname);
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);

// Free result set


mysqli_free_result($result);
mysqli_close($con);
?>
Modifying Data

The UPDATE statement is used to update existing records in a table:


Example :
Table name : "MyGuests"

id firstname lastname email

1 Ritvik Rishi [email protected]

2 Sachith Sanay [email protected]


The following examples update the record with id=2 in the "MyGuests" table:
Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

$sql = "UPDATE MyGuests SET lastname='Shiva' WHERE id=2";

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


echo "Record updated successfully";
} else {
echo "Error updating record: ";
}
$conn->close();
?>
After the record is updated, the table will look like this:
id firstname lastname email

1 Ritvik Rishi [email protected]

2 Sachith Sanay [email protected]


DELETING DATA
The DELETE statement is used to delete records from a table:

DELETE FROM table_name WHERE some_column = some_value

Example :
Table name : "MyGuests"
id firstname lastname email

1 Ritvik Rishi [email protected]

2 Sachith Sanay [email protected]

The following examples update the record with id=1 in the "MyGuests" table:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

// sql to delete a record


$sql = "DELETE FROM MyGuests WHERE id=1";

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


echo "Record deleted successfully";
} else {
echo "Error deleting record: ";
}

$conn->close();
?>

After the record is deleted, the table will look like this:

id firstname lastname email

2 Sachith Sanay [email protected]

You might also like