Lunanbay PHP HowTo

Invoking a PHP script is pretty straight forward once you've set PHP up.

Set PHP up

Invoking a PHP Script

In the first instance, name your file with a .php extension. Thereafter you insert code in a similar way to JSP. Some examples:

PHP info The basic script to tell you your PHP installation has worked
MySQL talk to your MySQL database

PHP Info

This PHP script should print out the basic info about your PHP installation.

<? phpinfo() ?>

Note the <? and ?> delimiters which introduce PHP code.

Talk to Your MySQL DB

Suppose you have set up your test_user identified by their test_password with access to your test_table in your test_db.

<?php $db = mysql_connect ("localhost", "test_user", "test_password") or die ("mysql_connect (localhost) failed:"); mysql_select_db("test_db") or die("Could not select test_db:"); $query = "SELECT * FROM test_table"; $result = mysql_query($query) or die("Query failed"); while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "row: "; foreach ($line as $col_value) { print "$col_value "; } print "\n"; } mysql_close($db); ?>