home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 February / INTERNET88.ISO / pc / html / php_code.txt < prev    next >
Encoding:
Text File  |  2001-12-18  |  1.9 KB  |  120 lines

  1. Code examples from PHP feature
  2. ------------------------------
  3.  
  4.  
  5.  
  6. [fig.1]
  7.  
  8. <html>
  9. <body>
  10. <P>This is a test of PHP. If you can't see anything below this line, PHP isn't working properly.</P>
  11.  
  12. <?php
  13. echo "PHP is working!";
  14. phpinfo();
  15. ?>
  16.  
  17. </body>
  18. </html>
  19.  
  20. [end of fig.1]
  21.  
  22.  
  23. [fig.2]
  24.  
  25. <html>
  26. <body>
  27. <P>Browser detection.</P>
  28.  
  29. <?php
  30. if(strstr($HTTP_USER_AGENT,"MSIE")) {
  31. ?>
  32.  
  33. <P>Internet Explorer detected. IE-specific HTML code goes here.</P>
  34.  
  35. <?
  36. } else {
  37. ?>
  38.  
  39. <P>Some other browser detected. Generic HTML code goes here.</P>
  40.  
  41. <?
  42. }
  43. ?>
  44.  
  45. </body>
  46. </html>
  47.  
  48. [end of fig.2]
  49.  
  50.  
  51. [fig.3]
  52.  
  53. <html>
  54. <body>
  55. <P>User-submitted variables.</P>
  56.  
  57. <form action="name.php" method="post">
  58. Your first name: <input type="text" name="firstname">
  59. Your surname: <input type="text" name="surname">
  60. <input type="submit">
  61. </form>
  62.  
  63. </body>
  64. </html>
  65.  
  66. [end of fig.3]
  67.  
  68.  
  69. [fig.4]
  70.  
  71. <P>Welcome. Your first name is  <?php echo $firstname; ?>.
  72. Your surname name is <?php echo $surname; ?>.</P>
  73.  
  74. [end of fig.4]
  75.  
  76.  
  77. [fig.5]
  78.  
  79. <html>
  80. <body>
  81. <P>Extracting values from the database.</P>
  82.  
  83. <!-- The next bit connects to the database (and won't be visible to a browser!) -->
  84. <!-- Use the same values for localhost, username and password as you did in phpMyAdmin -->
  85.  
  86. <?php
  87. $thisdb = mysql_connect("localhost", "username", "password");
  88. mysql_select_db("test",$thisdb);
  89. ?>
  90.  
  91. <!-- Next, use SQL to pick a table - ours is called 'birthdays' -->
  92.  
  93. <?php
  94. $sql="SELECT * FROM birthdays";
  95. $result=mysql_query($sql,$thisdb);
  96. ?>
  97.  
  98. <!-- Now extract the first row of that table and put it in an array -->
  99.  
  100. <?php
  101. $tablerow = mysql_fetch_array($result);
  102. ?>
  103.  
  104. <!-- Finally, extract individual variables from the array and print them -->
  105.  
  106. <?php
  107. $counter = $tablerow["counter"];
  108. $name = $tablerow["name"];
  109. $birthday = $tablerow["birthday"];?>
  110.  
  111. echo "Entry number $counter: <B>$name</B>'s birthday is $birthday<BR>";
  112.  
  113. ?>
  114.  
  115. </body>
  116. </html>
  117.  
  118. [end of fig.5]
  119.  
  120.