使用PHP进行数据库访问和动态Web内容的发送

ZDNet软件频道 时间:2002-02-05 作者:ZDNET CHINA 特稿 |  我要评论()
本文关键词:
在本文中,我将解释你如何能够使用PHP连接到一个数据库然后从它提取出信息。然后,我将向你们展示一个例子Web站点的页面,它使用一个MySQL 数据库来生成动态页面内容。为了我们演示的目的,我将假设你了解你的数据库的基本语法。
Listing A
<?php

## portal.php - sample page generation script
## for use with TechRepublic article "Dynamic Web with PHP"
## uses mySQL database
## this script pretends that a user named 'Eddie' has logged in,
## and that he has entered his user and config info

$portal_page = new Portal;

class Portal {

  function portal() {
      global $configs, $userinfo;
      $uname = "eddie";
      $dbid = $this->dbConnect();
      $configs = $this->runQuery($uname, "config");
      $userinfo = $this->runQuery($uname, "info");
      $this->showPage();
      mysql_close($dbid);
   }

## connect to db - this includes a check at the end to make sure the db tablespace we wanted is available
## '$db' is our database link identifier
  function dbConnect() {
      $dbhost = "localhost";
      $dbuname = "root";
     $dbpass = "";
      $dbname = "samples";
      $db = mysql_connect($dbhost, $dbuname, $dbpass);
      @mysql_select_db("$dbname") or die ("Unable to select database");
      return($db);
   }

## query the database for either user configuration information, or for stored user data
  function runQuery($user, $query_type) {
      switch($query_type) {
         case "config" :
            $table = "configs";
            break;
         case "info" :
            $table = "userdata";
            break;
     }
      $prefs = mysql_query("SELECT * FROM $table where uname = '$user'");
      while ($row = mysql_fetch_array($prefs, MYSQL_ASSOC)) {
         $data = $row;
      }
      return($data);
   }

## draw the Web page, using data retrieved from the database
  function showPage() {
      global $configs, $userinfo;
?>
<html>
<head>
<title>portal.php - sample script</title>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 rightmargin=0 topmargin=0 bgcolor=white>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
<tr><td colspan=3>
<?
## show the header in the user's style
      $header = $configs[style]."_header.php";
      require $header;
?>
</td></tr> <tr valign=top><td width="20%"><table bgcolor="#000000" border=0 cellpadding=5 cellspacing=0 valign=top>
<?
## show links the user configured to see
      if ($configs[favlinks] == '1') {
         require "favlinks.php";
      }
      if ($configs[phplinks] == '1') {
         require "phplinks.php";
      }
?>
</table></td> <td width="60%">
<table border=0 cellpadding=5 cellspacing=0 valign=top><tr><td valign=top bgcolor="<?
## use the user's favorite color for the background
      echo $configs[favcolor];
?>">
<?
## show the user's picture
      if ($configs[picture]) {
         echo "<img src="./".$configs[picture]."" width="200">";
      }
?>
</td><td>
<b><font size="+1">About <? echo $userinfo[first]; ?></font></b><br><br>
<?
## display bio information from the database
      echo $userinfo[bio];
?>
</td></tr></table></td>
<td width="20%"><table border=0 cellpadding=5 cellspacing=0 valign=top>
<?
      if ($configs[goodlinks] == '1') {
         require "goodlinks.php";
      }
      if ($configs[badlinks] == '1') {
         require "badlinks.php";
      }
?>
</table></td></tr>
<tr><td align=center colspan=3>
<?
      echo $userinfo[first]." ".$userinfo[last]." - ".$userinfo[address]." - ".$userinfo[phone]." - <a href="mailto:".$userinfo[email]."">".$userinfo[email]."</a>";
?>
</td></tr>
</table>
</body>
</html>
<?
   }

}

?>

百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134