Systems Status

Announce? RSS Feed RSS

Blog?RSS Feed RSS

Help Topics

Deschutes Facilities

Campus Facilities

User Account Info

Roundcube Mail

Contact Systems

ipv6 ready

Misc

Introduction

You can access a MySQL database from PHP. The MySQL daemon allows connections via unix sockets and TCP sockets. The easiest way for students to connect their web pages to their database is via unix sockets. That just involves hard-coding a string in your mysql_connect() call. If you are using our mysqlctl, the TCP port that your MySQL server listens on may change each time mysqld starts. Therefore you should avoid hard coding the port and server name into your PHP. It is easy to have PHP parse your .my.cnf file and extract that information. Also if your database is password protected you may not want the password to be embeded in your PHP script. You can use the same PHP function to parse an application configuration file that you set up for your PHP application.

The PHP function that parses .my.cnf and any other configuration file that is in the format of "variable = value" is parse_ini_file. This funcion reads the "ini" file and returns the settings in an associative array. Read the manual page for more details http://www.php.net/manual/en/function.parse-ini-file.php. Note, that in php-5.3 parse_ini_file will no longer allow # based comments. You need to strip these out, and change to parse_ini_string.

PHP code example: myapp.cgi

#!/local/bin/php
<?php

$homedir = "/home/users/jdash";

// Write out a valid HTML header
print <<<end_of_header
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang='en'><head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Example of MySQL with PHP</title>
</head><body>
end
_of_header
;

// Parse the config file for this app; this one in the current directory
$conf = @parse_ini_file("$homedir/etc/myapp.conf");
$debug = $conf['debug'];

// student users can use unix domain socket to connect
$location = ":$homedir/mysql-data/mysqld_sock";

// if for some reason your database is on a different server, parse .my.cnf
$mysql_conf = parse_ini_string(preg_replace("/^#.*$/m","", file_get_contents("$homedir/.my.cnf")), TRUE);
$location = $mysql_conf['client']['host'].":".$mysql_conf['client']['port'];

// connect to the database server
$conn = mysql_connect($location, $conf['dbuser'], $conf['password'])
        or die ("Could not connect MySQL");
if ($debug) print "Connection to <b>$location</b> successful.<br />";

// select the database
$selected_db=mysql_select_db($conf['database'], $conn)
        or die ("Could not open database");
if ($debug) print "Database <b>$database</b> selected.<br />";

// Now you are ready to use mysql_query() and mysql_fetch()

mysql_close($conn);
print "</body></html>";
?>

~/etc/myapp.conf file

; this file contains the configuration unique to myapp
; each application probably uses a different database user/pasword
dbuser = myapp-user
database = my_db
password = 7lu663r
debug = true

You should make sure that myapp.conf is not readable by others to protect your password (700).

See Also

Edited: November 05, 2014, at 09:45 am
Copyright © 2024, University of Oregon, All rights reserved
Privacy Policy