|
|
|
|
|
|
PHP, DBs and Application
Development |
|||
PHP, DBs and Application
Development
PSU CS 386 - Introduction to Databases
Most slides used courtesy of Kim Howard and Len
Shapiro
OUTLINE
·
Setting up an
environment
·
Overview of PHP
and HTML
·
PHP Syntax and
Semantics
·
HTML Forms
·
PHP &
Databases
§
Postgres and MySQL
·
References
Obtain A CS Unix Account
Tutors in CS UNIX lab : FAB 135
TheCAT
FAB 60-08 (Doghouse)
CAT
Front Desks And Hours: http://www.cat.pdx.edu/students/labs.php
Questions and
problems can be addressed by Tutors, Front Desk Workers or by email to
support@cat.pdx.edu
chmod 644 index.html



If your HTML/PHP
file contains:
<html>
<?php echo "Hello World"?>
</html>
<html>
Hello
World
</html>
index.php
<html>
<?php phpinfo(); ?>
</html>
|
System |
SunOS
moosehead.cat.pdx.edu 5.9 Generic_118558-09 sun4u |
|
Build Date |
|
|
Configure Command |
….omitted…. |
|
Server API |
Apache
|
|
Virtual Directory Support |
disabled
|
|
Configuration File (php.ini) Path |
/pkgs/webservers/apache-1.3.33.test/etc
|
|
PHP API |
20020918
|
|
PHP Extension |
20020429
|
|
Zend Extension |
20021010
|
|
Debug Build |
no
|
|
Thread Safety |
disabled
|
|
Registered PHP Streams |
php, http, ftp, https, ftps,
compress.zlib |
Tag Styles
<html><body>
<? echo 'Short Tags - Most common' ?>
<br
/>
<?php echo 'Long Tags - Portable, Highly Recommended' ?>
<br
/>
<script
language="php">
echo 'Really Long Tags - rarely used';
</script>
<br
/>
</body></html>
Short
Tags - Most common
Long Tags - Portable, Highly Recommended
Really Long Tags - rarely used
PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments
<?php
$variable
= "foobar"; // comment here
/*
multi
line
comment
Don't
nest these. */
#also a comment
?>
Like C or Perl, end each
instruction with a semi-colon (;)
<?php $variable = "Whee....
stuff goes here"; ?>
<?php echo "$variable"
// closing tag
implies a semicolon ?>
Whee.... stuff goes here
<?php
$foo = 1; // integer
$bar = "Testing"; // string
$xyz = 3.14; // float
$foo = $foo + 1; // still an integer
?>
Numbers (integers and real),
Strings, Booleans
<?php
$a
= 1234;
$b = 0777;
$c = 0xff;
$d = 1.25;
echo
"$a
$b $c $d<br />\n";
?>
<?php
$name
= 'Rasmus $last'; // Single-quoted
$str = "Hi $name\n"; // Double-quoted
echo $str;
?>
<?php
$greeting
= true;
while
($greeting) {
echo
"Hi
Carl";
$greeting = false;
}
?>
Dynamic Typing
<?php
echo 5 + "1.5" + "10e2";
?>
<?php
echo 'this is a simple string<br/>';
$foo
= "bar";
echo "This should say bar: $foo<br/>";
echo 'This should say $foo:
$foo<br/>';
?>
This should say bar: bar
This should say $foo: $foo
<?php
$arr[1] = 1;
$arr["stuff"] = "more stuff"; // always use quotes around string
literal array indexes
$arr2[1][2] = 3;
?>
Superglobals
<?php
echo "script: $_SERVER[SCRIPT_NAME]
<br/>\n";
echo
"user-agent:
$_SERVER[HTTP_USER_AGENT] <br/>\n";
?>
script: /~johnj/talks/show.php
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
<?php
$loop = 0;
$i
= 0;
for ($loop = -3; $loop < 3; $loop++) {
if
($i < 0)
{
echo
"-";
}
elseif ($i > 0) {
echo
"+";
}
echo
"$loop<br/>\n";
}
while(--$loop) {
switch($loop % 2) {
case
0:
echo
"Even<br/>\n";
break;
case
1:
echo
"Odd<br/>\n";
break;
}
}
do {
echo
"$loop<br/>";
} while
(++$loop
< 5);
?>
-3
-2
-1
0
1
2
Even
Odd
0
1
2
3
4
<form action="<?php echo $PHP_SELF ?>" method="POST">
Your
name: <input type="text" name="name"><br>
Your
age: <input type="text" name="age"><br>
<input
type="submit">
</form>
Hi <?echo $_POST['name'] ?>.
You are
<?echo $_POST['age'] ?> years old.
<?php
$username
= "introdb_readonly";
$password = "introdb";
$databasename
= "introdb_library";
$hostname = "db.cecs.pdx.edu";
$connection = pg_connect("host=$hostname dbname=$databasename user=$username password=$password")
or
die ("Could
not connect");
// do stuff
pg_close($connection);
?>
<?php
$username
= "introdb_readonly";
$password = "foobar"; // wrong password
$databasename
= "introdb_library";
$hostname = "db.cecs.pdx.edu";
$connection = pg_connect("host=$hostname dbname=$databasename user=$username password=$password")
or
die ("Could
not connect");
// do stuff
pg_close($connection);?>
Warning: pg_connect(): Unable to connect to PostgreSQL
server:
FATAL:
Password authentication failed for user "introdb_readonly"
in ./display.php(433) : eval()'d
code on line 7
Could
not connect
<?php
// assuming we
already connected with code from previous slide...
$query = "
SELECT
DISTINCT A.FirstName, A.LastName
FROM
Author A
WHERE A.AuthorID IN (
SELECT
RA.AuthorID
FROM
RelAuth RA
WHERE
RA.Role = 'Translator'
);
";
$result = pg_query($connection, $query) or die("Query error: " . pg_last_error());
?>
<?php
// assuming we
already connected with code from previous slide...
$query = "
SELECT
DISTINCT foobar
FROM
Author A
WHERE A.AuthorID IN (
SELECT
RA.AuthorID
FROM
RelAuth RA
WHERE
RA.Role = 'Translator'
);
";
$result = pg_query($connection, $query) or die("Query error: " . pg_last_error());
?>
Warning: pg_query(): Query failed:
ERROR: column "foobar" does not exist in ./display.php(433) :
eval()'d code on line 20
Query
error: ERROR: column "foobar" does not
exist
pg_fetch_row() returns a row as an
enumerated array
<?php
echo "<strong>" . pg_num_rows($result) . " rows
returned</strong><hr>\n";
$row = 0;
while ($row = pg_fetch_row($result)) {
echo
"First
Name: $row[0]<br/>\n";
echo
"Last
Name: $row[1]<br/>\n";
echo
"<hr>\n";
}
?>
First Name: J
Last Name: FOSTER
First Name:
Last Name: CHUA
First Name: OREN
Last Name: PATASHNIK
First Name: T
Last Name:
First Name: YAOHAN
Last Name:
<?php
$username
= "introdb_readonly";
$password = "introdb";
$databasename
= "introdb_library";
$hostname = "db.cecs.pdx.edu";
$connection = pg_connect("host=$hostname dbname=$databasename user=$username password=$password")
or
die ("Could
not connect");
$query = "
SELECT
DISTINCT A.FirstName, A.LastName
FROM
Author A
WHERE A.AuthorID IN (
SELECT
RA.AuthorID
FROM
RelAuth RA
WHERE
RA.Role = 'Translator'
);
";
$result = pg_query($connection, $query) or die("Query error: " . pg_last_error());
$row = 0;
echo "<strong>" . pg_num_rows($result) . " rows
returned</strong><hr>\n";
while ($row = pg_fetch_row($result)) {
echo
"First
Name: $row[0]<br/>\n";
echo
"Last
Name: $row[1]<br/>\n";
echo
"<hr>\n";
}
pg_close($connection);
?>
First Name: J
Last Name: FOSTER
…
<?php
$username
= "akimbo_ro";
$password = "readme";
$databasename
= "akimbo";
$hostname = "db.cecs.pdx.edu";
$connection = mysql_connect($hostname, $username, $password)
or
die ("Could
not connect");
mysql_select_db($databasename) or die("Database select error: " . mysql_error());
$query = "
SELECT
DISTINCT A.FirstName, A.LastName
FROM
Author A
WHERE A.AuthorID IN (
SELECT
RA.AuthorID
FROM
RelAuth RA
WHERE
RA.Role = 'Translator'
);
";
$result = mysql_query($query, $connection) or die("Query error: " . mysql_error());
echo "<strong>" . mysql_num_rows($result) . " rows
returned</strong><hr>\n";
while ($row = mysql_fetch_row($result)) {
echo
"First
Name: $row[0]<br/>\n";
echo
"Last
Name: $row[1]<br/>\n";
echo
"<hr>\n";
}
mysql_close($connection);
?>
First Name: T
Last Name:
PHP Website, Manual: http://www.php.net
PHP Beginning Tutorial: http://www.php.net/tut.php
Zend Website: http://www.zend.com/developers.php
Zend Beginning PHP Articles: http://www.zend.com/php/beginners/index.php
PHP Presentations: http://talks.php.net/
More PHP Related Links http://www.php.net/links.php