Valid
	XHTML 1.1! Valid CSS!
Created 2008-02-23   Modified 2009-04-11
Chelton Evans

Java Script phpnotes php home

Command line script
Connecting to a database and querying
Simple State Control
Configuration
My Development

Examples

File Uploads   AJAX tutorial   Php's html, header and footers  

Command line script

Executing php as a command line script by enclosing in tags and -f option. eg
php -f p004.php
where the p004.php file :
<?
$a1 = "****";
echo "$a1";
?>

prints
****

To access the os use the exec command. Note I do not use escapeshellcmd because it broke on some system calls. Not using it removes one level of complexity, but now you have to manage special characters yourself.
<?php
exec("ps -Al | grep scsi",$out);
echo join("\n",$out);
?>

References
function f1($&str, $s1, $s2)
{
    $str = implode($s1,$s2);
}
Reference on return value of function
function &f2( ...

Connecting to a database and querying

<?
# Supply the user and passord to createt the database connection.
$con = mysql_connect("localhost",'libraryuser','****');
$db = "library";
mysql_select_db($db,$con)
or die("error: can not open $db");

$sql = "select * from books";
$res =
mysql_query($sql,$con)
or die("error: could not execute sql: $sql");

$res_num = mysql_num_rows($res);
echo "Book's table display\n\n";

# The row fields are case sensitive
for ($i=0; $i>$res_num; $i++)
{
$row = mysql_fetch_array($res);
$isbn = $row["ISBN"];
echo "ISBN=$isbn ";

$title = $row["title"];
echo "title=$title ";

$publisher = $row["publisher"];
echo "publisher=$publisher";
echo "\n";
}
mysql_close($con);

?>

Simple State Control

The gui is essentially html. Here is an example where the user has a few buttons to change a table. Each button fires to a unique page to control the state. The common code is put into a function and included/required.

This is refered to the blink strategy, because you have to wait till the page loads every time.

Here is sorting by title page. The other sorting pages are similar.
<html>
<body>
<h1>php button test</h1>
<?php
require 'p008libmain.inc';
books_table("select * from books order by title");
?>
</body>
</html>

<?php
function books_table($sql)
{
$con = mysql_connect("localhost",'libraryuser','****');
$db = "library";
mysql_select_db($db,$con)
or die("error: can not open $db");
$res = mysql_query($sql,$con)
or die("error: could not execute sql: $sql");
$res_num = mysql_num_rows($res);
echo "<h2> Books table </h2>";
echo "<table>";
echo "<tr>";
echo "<td> <form action=\"p008ISBN.php\" method=\"POST\">";
echo "<input type=\"submit\" value=\"ISBN\" />";
echo "</form> </td>";
echo "<td> <form action=\"p008Title.php\" method=\"POST\">";
echo "<input type=\"submit\" value=\"Title\" />";
echo "</form> </td>";
echo "<td> <form action=\"p008Publisher.php\" method=\"POST\">";
echo "<input type=\"submit\" value=\"Publisher\" />";
echo "</form> </td>";
echo "<td> <form action=\"p008Category.php\" method=\"POST\">";
echo "<input type=\"submit\" value=\"Category\" />";
echo "</form> </td>";
echo "</tr>";
for ($i=0; $i<$res_num; $i++)
{
echo "<tr>";
$row = mysql_fetch_array($res);
echo "<td>";
$isbn = $row["ISBN"];
echo "$isbn ";
echo "</td>";
echo "<td>";
$title = $row["title"];
echo "$title ";
echo "</td>";
echo "<td>";
$publisher = $row["publisher"];
echo "$publisher ";
echo "</td>";
echo "<td>";
$category = $row["category"];
echo "$category";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

Configuration

# /usr/sbin/apachectl
/etc/httpd/conf/conf/httpd.conf
# CE 2008-02-23
# Enable php
AddType application/x-httpd-php .php
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php
# Disallow reading .inc files
<Files ~ "\.inc$">
    Order allow,deny
    Deny from all
</Files>

To restart httpd service
KDE > system > services > select httpd and click restart

My Development

Mac set up with symlink to p1/comsci/php, eg
http://localhost/~zero/php/html/p008.php
Linux box set up differently, I do not have uniform databases so some only work on linux box...