User Tools

Site Tools


linux:php-lamp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

linux:php-lamp [2019/01/21 11:26] (current)
seanburns created
Line 1: Line 1:
 +<markdown>
 +# 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.
 +
 +
 +```bash
 +# 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).
 +
 +```bash
 +# cd /var/www/html/
 +# nano login.php
 +```
 +
 +In the file, add the following credentials, substituting your credentials where
 +necessary:
 +
 +```php
 +<?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:
 +
 +```php
 +<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:
 +
 +```bash
 +# 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
 +
 +- [How to Test PHP MySQL Database Connection Using Script][1]
 +- [Install Apache/PHP 7.2.12 on Fedora 29/28, CentOS/RHEL 7.5/6.10][2]
 +- [MySQL Improved Extension][3]
 +- [PHP 5 MySQLi Functions][4]
 +
 +[1]:https://www.tecmint.com/test-php-mysql-database-connection-using-script/
 +[2]:https://www.if-not-true-then-false.com/2010/install-apache-php-on-fedora-centos-red-hat-rhel/
 +[3]:https://secure.php.net/manual/en/book.mysqli.php
 +[4]:https://www.w3schools.com/PHP/php_ref_mysqli.asp
 +</markdown>
linux/php-lamp.txt ยท Last modified: 2019/01/21 11:26 by seanburns