User Tools

Site Tools


linux:php-lamp

Set up PHP and MySQL

Date: Tue 13 Nov 2018

Install PHP and MySQL Support

First install MySQL support for PHP. We're installing some modules alongside the basic support. These may or may not be needed, but I'm installing them to demonstrate some basics. Use dnf info <packagename> to get information about each package before installing.

# dnf install php-mysqlnd php-gd php-cli php-mbstring
# systemctl restart mysqld.service
# systemctl restart httpd.service

Create PHP Scripts

Now let's move to the base HTML directory and create our login file, which will contain the credentials for our MySQL accounts. In previous weeks, I demonstrated virtualhosts. Here I'm working with a base install (no virtualhosts configured).

# cd /var/www/html/
# nano login.php

In the file, add the following credentials, substituting your credentials where necessary:

<?php // login.php
$db_hostname = "localhost";
$db_database = "pets";
$db_username = "sean";
$db_password = "an0ldP4ssPhrase!";
?>

Now, in a separate file, which I'll call pets.php, I'll add the following PHP to test our database connection and return some results:

<html>
<head><title>MySQL Server Example</title></head>
<body>

<?php

// Load MySQL credentials
require_once 'login.php';

// Establish connection
$conn = mysqli_connect($db_hostname, $db_username, $db_password) or
    die("Unable to connect");

// Open database
mysqli_select_db($conn, $db_database) or
    die("Could not open database '$db_database'");

// QUERY 1
$query1 = "show tables from $db_database";
$result1 = mysqli_query($conn, $query1);

$tblcnt = 0;
while($tbl = mysqli_fetch_array($result1)) {
    $tblcnt++;
}

if (!$tblcnt) {
    echo "<p>There are no tables</p>\n";
}
else {
    echo "<p>There are $tblcnt tables</p>\n";
}

// Free result1 set
mysqli_free_result($result1);

// QUERY 2
$query2 = "select name, owner from cats";
$result2 = mysqli_query($conn, $query2);

$row = mysqli_fetch_array($result2, MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);
echo "<br/>";

$row = mysqli_fetch_array($result2, MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["name"], $row["owner"]);

// Free result2 set  
mysqli_free_result($result2);

// Query 3
$query3 = "select * from cats";
$result3 = mysqli_query($conn, $query3);

while($row = $result3->fetch_assoc()) {
    echo "<p>Owner " . $row["owner"] . " has pet " . $row["name"] . ".</p>";
}

mysqli_free_result($result3);

$result4 = mysqli_query($conn, $query3);
while($row = $result4->fetch_assoc()) {
    echo "<p>Pet " . $row["name"] . " was born on " . $row["birth"] . ".</p>";
}

// Free result4 set  
mysqli_free_result($result4);

/* Close connection */
mysqli_close($conn);

?>

</body>
</html>

After you save the file, change the ownership:

# chown apache:apache pets.php
# chown apache:apache login.php

Tasks

Copy the login.php and pets.php to your public_html directory (you should still have userdir enabled). Figure out what you need to change in order to get your script to work there.

References

linux/php-lamp.txt · Last modified: 2019/01/21 11:26 by seanburns