0% found this document useful (0 votes)
18 views5 pages

Code

The document provides instructions for installing and configuring an Apache web server with PHP and connecting it to a MySQL database. It includes steps to install Apache and PHP, set file permissions, define database connection details, create a PHP file to connect to the database and display data, and includes functions to add data and check/create the database table.

Uploaded by

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

Code

The document provides instructions for installing and configuring an Apache web server with PHP and connecting it to a MySQL database. It includes steps to install Apache and PHP, set file permissions, define database connection details, create a PHP file to connect to the database and display data, and includes functions to add data and check/create the database table.

Uploaded by

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

QEG0NPXSJ6VG6UYOQ9

Code used in Project

Install Apache server with PHP

Below are the steps/commands to complete the process

sudo yum update -y

sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2

sudo yum install -y httpd

sudo systemctl start httpd

sudo systemctl enable httpd

To set file permissions for the Apache web server

1.sudo usermod -a -G apache ec2-user

2. sudo chown -R ec2-user:apache /var/www

3. sudo chmod 2775 /var/www

4.find /var/www -type d -exec sudo chmod 2775 {} \;

5. find /var/www -type f -exec sudo chmod 0664 {} \;

Connect Apache web server to DB instance

<?php

define('DB_SERVER', ‘mydbserver’);

define('DB_USERNAME', 'admin');

define('DB_PASSWORD', 'Qwerty.123');

define('DB_DATABASE', 'myrds-db');

?>

Now Navigate to /var/www/html and create UserData.php file with below content
<?php include "../inc/dbinfo.inc"; ?>

<html>

<body>

<h1>User Data</h1>

<?php

/* Connect to MySQL and select the database. */

$connection = mysqli_connect(mydbserver, admin, Qwerty.123);

if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

$database = mysqli_select_db($connection, myrds-db);

/* Ensure that the EMPLOYEES table exists. */

VerifyEmployeesTable($connection, myrds-db);

/* If input fields are populated, add a row to the EMPLOYEES table. */

$employee_name = htmlentities($_POST['NAME']);

$employee_address = htmlentities($_POST['ADDRESS']);

if (strlen($employee_name) || strlen($employee_address)) {

AddEmployee($connection, $employee_name, $employee_address);

?>

<!-- Input form -->

<form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">

<table border="0">

<tr>

<td>NAME</td>

<td>ADDRESS</td>

</tr>

<tr>

<td>

<input type="text" name="NAME" maxlength="45" size="30" />

</td>

<td>
<input type="text" name="ADDRESS" maxlength="90" size="60" />

</td>

<td>

<input type="submit" value="Add Data" />

</td>

</tr>

</table>

</form>

<!-- Display table data. -->

<table border="1" cellpadding="2" cellspacing="2">

<tr>

<td>ID</td>

<td>NAME</td>

<td>ADDRESS</td>

</tr>

<?php

$result = mysqli_query($connection, "SELECT * FROM EMPLOYEES");

while($query_data = mysqli_fetch_row($result)) {

echo "<tr>";

echo "<td>",$query_data[0], "</td>",

"<td>",$query_data[1], "</td>",

"<td>",$query_data[2], "</td>";

echo "</tr>";

?>

</table>

<!-- Clean up. -->

<?php

mysqli_free_result($result);

mysqli_close($connection);

?>
</body>

</html>

<?php

/* Add an employee to the table. */

function AddEmployee($connection, $name, $address) {

$n = mysqli_real_escape_string($connection, $name);

$a = mysqli_real_escape_string($connection, $address);

$query = "INSERT INTO EMPLOYEES (NAME, ADDRESS) VALUES ('$n', '$a');";

if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");

/* Check whether the table exists and, if not, create it. */

function VerifyEmployeesTable($connection, $dbName) {

if(!TableExists("EMPLOYEES", $connection, $dbName))

$query = "CREATE TABLE EMPLOYEES (

ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

NAME VARCHAR(45),

ADDRESS VARCHAR(90)

)";

if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");

/* Check for the existence of a table. */

function TableExists($tableName, $connection, $dbName) {

$t = mysqli_real_escape_string($connection, $tableName);

$d = mysqli_real_escape_string($connection, $dbName);

$checktable = mysqli_query($connection,

"SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND

TABLE_SCHEMA = '$d'");

if(mysqli_num_rows($checktable) > 0) return true;

return false;
}

?>

You might also like