100 likes | 278 Views
CS242. PHP. PHP. PHP - hypertext pre-processor PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML PHP is a server-side technology. That is, action is taken by the server, not the user's computer. What you need.
E N D
CS242 PHP
PHP • PHP - hypertext pre-processor • PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML • PHP is a server-side technology. That is, action is taken by the server, not the user's computer.
What you need • server like Apache • PHP • recommended: MySQL
Example PHP/HTML <html><head><title>PHP Test</title></head><body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
View-> Source(Output) <html> <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body> </html>
Connecting to a database $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employees",$db);
Results of a Query $result = odbc_exec($db,$query); if ($myrow = odbc_fetch_array($result)) { // output HTML code here to begin the table // echo "<table border=1 bgcolor=\"#666666\">\n"; do { printf("<tr bgcolor=\"#999999\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow ["date"], $myrow["time"], $myrow["Slot 1"], $myrow["Slot 2"], $myrow["Slot 3"]); $myrow = odbc_fetch_array($result); } while ($myrow["date"]!=""); // end the table started above // echo "</table>"; }
Result Sets <?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employees",$db); echo "<table border=1>\n"; echo "<tr><td>Name</td><td>Position</tr>\n"; while ($myrow = mysql_fetch_row($result)) { printf("<tr><td>%s %s</td><td>%s</td></tr>\n", $myrow[1], $myrow[2], $myrow[3]); } echo "</table>\n"; ?>
Using a Form to get data <form action="action.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your age: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form>
$PHP_SELF The filename of the currently executing script, relative to the document root. http://www.phpfreaks.com/phpref/34.php $current_url = $_SERVER['PHP_SELF']; Then $current_url is “phpref/34.php”