PHP

From "A B C"
Jump to navigation Jump to search

PHP


The contents of this page has recently been imported from an older version of this Wiki. This page may contain outdated information, information that is irrelevant for this Wiki, information that needs to be differently structured, outdated syntax, and/or broken links. Use with caution!


Summary ...



 

Introductory reading



 

Contents

Configuring apache for PHP

Of course, the first prerequisite is that php is actually installed on your system. Type

$ which php

The configuration of apache to execute php files can be done similar to the perl/cgi configuration.

  • Navigate to the apache configuration directory. If this is a default installation, type
$ cd /usr/local/apache2/conf
  • Make a backup copy of the file httpd.conf
$ sudo cp httpd.conf httpd.conf.2
  • Open httpd.conf for editing
$ pico httpd.conf
  • You have already told apache that .pl files are executable. All you need to do is to add .php to that directive. Change ...
AddHandler cgi-script .pl

... to...

AddHandler cgi-script .pl .php

... and save the modified file.

  • Finally, restart apache to use the new configuration
$ sudo /usr/local/apache2/bin/apachctl restart

Testing the Configuration

The simplest possible PHP script is not actually a HTML document. Create a file phpinfo.php containing the following bit of php code:

<? phpinfo(); ?>

This calls a function within PHP that writes out HTML code for a complete overview of the PHP installation.

For a simple script that demonstrates the interaction of PHP with HTML, try the following:

<html>
<head>
<title>test</title>
</head>
<body>
<?php print "<h1>Hello</h1>\n"; ?>
</body>
</html>

Testing Communication with MySQL

Finding out the mysqld port:

/usr/local/mysql/bin/mysqladmin -u developer -p variables

The port is listed in the row "port" and by default would be 3306.

Reference: Troubleshooting "Can't connect ..."


Try the following piece of code - either by asking apache to serve this document to a Web-browser (if you have successfully configured apache to handle php documents) OR by running the code through Komodo, which will write the html code to stdout:

<html>
<head><title>Test PHP -> MySQL connection</title></head>
<body>
<?php

$db="localhost";
$port="3306";
$user="developer";
$password="mysecretpassword";

// connect and select a database 
$link = mysql_connect($db . ":" . $port, $user, $password)
or die ("Couldn't connect:  Check to make sure that:<br>" .
		"<ul><li>your MySQL server is running</li>" .
		"<li>you used the correct hostname<li>" .      
		"<li>you checked that the port is correct<li>" .      
		"<li>you used a username that is recognized by this mysql server</li>" .       
		"<li>you used the correct password</li>");
print "Connected successfully to $db on port $port as user $user.\n<p>\n";

// perform an SQL query 
$sql_command = "SHOW DATABASES";
$result = mysql_query($sql_command, $link) or die("Query failed");

?>
<h3>Databases available to this user:</h3>
<p>
<table border="1">
<?php

while ($row = mysql_fetch_assoc($result)) {
    echo "<tr><td>" . $row['Database'] . "</td></tr>\n";
}

?>
</table>
<?php

// free result set 
mysql_free_result($result);

// close the connection 
mysql_close($link);

?>
</body>
</html>



   

Further reading and resources