How to Connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO
- Mohammad Abu Mattar
- Cloud Computing
- 07 Nov, 2022
- 08 Mins read
Introduction
In this post, we will learn how to connect to AWS RDS MySQL Database to EC2 Instance With PHP By Using PDO.
Prerequisites
- AWS Account
- EC2 Instance Amazon Linux 2
- RDS MySQL Database
- MySQL Workbench
- SSH Client
Note: In previous posts, we have learned how to create a AWS RDS MySQL Database and connect to it using MySQL Workbench. If you have not read the previous posts, you can read them from the following links: How to Create a AWS RDS MySQL Database and Connect to it Using MySQL Workbench
Note: In previous posts, we have learned how to create a AWS EC2 Instance Amazon Linux 2. If you have not read the previous posts, you can read them from the following links: How To Create An AWS EC2 Instance Using AWS CLI
Create a PHP File structure
Connect RDS MySQL Database to EC2 Instance With PHP By Using PDO
Create a connect.php file
We will create a connect.php
file in the /var/www/html
directory.
Explanation:
Click to expand the explanation
$servername
- The server name of the RDS MySQL Database.$username
- The username of the RDS MySQL Database.$password
- The password of the RDS MySQL Database.$dbname
- The database name of the RDS MySQL Database.$port
- The port of the RDS MySQL Database.$dsn
- The Data Source Name, or DSN, contains the information required to connect to the database.$conn
- The connection object.PDO::ATTR_ERRMODE
- The error reporting attribute.PDO::ERRMODE_EXCEPTION
- Throw exceptions for errors.
Create a insert.php file
We will create a insert.php
file in the /var/www/html
directory.
Explanation:
Click to expand the explanation
include_once '../config/connect.php'
- Include theconnect.php
file.$_POST['bid']
- Get thebid
value from thePOST
request.$_POST['bname']
- Get thebname
value from thePOST
request.$_POST['color']
- Get thecolor
value from thePOST
request.$search = $conn->prepare()
- Prepare the SQL statement.$search->bindParam(':bid', $bid)
- Bind thebid
value to the:bid
parameter.$search->bindParam(':bname', $bname)
- Bind thebname
value to the:bname
parameter.$search->bindParam(':color', $color)
- Bind thecolor
value to the:color
parameter.$search->execute()
- Execute the SQL statement.$conn = null
- Close the connection.echo "Connection failed: " . $e->getMessage()
- Print the error message.
Create a index.php file
We will create a index.php
file in the /var/www/html
directory. We will conneact to DB using connect.php
file.
Click to expand the explanation
- Select all columns from the
SAILORS
table
- Select all columns from the
BOATS
table
- Select all columns from the
RESERVES
table
- Select all columns from the
SAILORS
table where theSID
is in theSID
column of theRESERVES
table where theBID
is equal to101
- Select the
SName
column from theSAILORS
table where theSID
is in theSID
column of theRESERVES
table where theBID
is in theBID
column of theBOATS
table where theBColor
is equal to'red'
and order the results by theSAge
column
- Select the
SName
column from theSAILORS
table where theSID
is in theSID
column of theRESERVES
table
- Select the
SID
andSName
columns from theSAILORS
table where theSID
is in theSID
column of theRESERVES
table grouped by theSID
andDay
columns having a count of*
greater than or equal to2
- Select the
SID
column from theSAILORS
table where theSID
is in theSID
column of theRESERVES
table where theBID
is in theBID
column of theBOATS
table where theBColor
is equal to'red'
or theSID
is in theSID
column of theRESERVES
table where theBID
is in theBID
column of theBOATS
table where theBColor
is equal to'green'
-
$result =$conn->query($query)
executes the query and returns the result as amysqlPDOStatement
object which can be used to fetch the results as an associative array using thefetchAll()
method -
$result->fetchAll();
fetches all the results as an associative array and returns it -
<?php foreach ($results as $row): ?>
loops through the results and assigns each row to the$row
variable -
<?php echo $row['column_name'] ?>
prints the value of the column with the namecolumn_name
in the current row -
<?php endforeach; ?>
ends the loop
Create a form to insert a new row into the BOATS
table
We will create a new file insert-boats-form.php
in the /var/www/html
directory and add the following code to it.
Explanation:
Click to expand the explanation
- The
action
attribute of the form is set toinsert-boats.php
which is the file that will handle the form submission - The
method
attribute of the form is set topost
which means that the form data will be sent to the server using thePOST
method - The
id
attribute of the input elements are set to the column names of theBOATS
table - The
name
attribute of the input elements are set to the column names of theBOATS
table - The
type
attribute of the input elements are set totext
which means that the input will be a text input - The
placeholder
attribute of the input elements are set to the column names of theBOATS
table - The
type
attribute of the submit button is set tosubmit
which means that the button will submit the form
Conclusion
In this tutorial, How to connect to a MySQL database using PHP was explained. We also learned how to create a form to insert data into a MySQL database using PHP.