返回列表 发帖

用 PHP 走向动态3 -- 经典的数据库访问

先从一个简单的数据库模式开始。清单 1 所示的模式针对的是单一的数据表数据库,容纳图书列表。


清单 1. MySQL 数据库模式
  1. DROP TABLE IF EXISTS book;
  2. CREATE TABLE book (
  3.         book_id INT NOT NULL AUTO_INCREMENT,
  4.         title TEXT,
  5.         publisher TEXT,
  6.         author TEXT,
  7.         PRIMARY KEY( book_id )
  8. );
复制代码
请把这个模式装入到名为 bookdb 的数据库。

接下来,编写一个常规的数据库类,然后再把它修改成动态的。清单 2 显示了图书表的简单的数据库访问类。


清单 2. 基本的数据库访问客户机
  1. <?php
  2. require_once("DB.php");
  3. $dsn = 'mysql://root:password@localhost/bookdb';
  4. $db =& DB::Connect( $dsn, array() );
  5. if (PEAR::isError($db)) { die($db->getMessage()); }
  6. class Book
  7. {
  8.   private $book_id;
  9.   private $title;
  10.   private $author;
  11.   private $publisher;
  12.   function __construct()
  13.   {
  14.   }
  15.   function set_title( $title ) { $this->title = $title; }
  16.   function get_title( ) { return $this->title; }
  17.   function set_author( $author ) { $this->author = $author; }
  18.   function get_author( ) { return $this->author; }
  19.   function set_publisher( $publisher ) {
  20.   $this->publisher = $publisher; }
  21.   function get_publisher( ) { return $this->publisher; }
  22.   function load( $id )
  23.   {
  24.     global $db;
  25. $res = $db->query( "SELECT * FROM book WHERE book_id=?",
  26.     array( $id ) );
  27.     $res->fetchInto( $row, DB_FETCHMODE_ASSOC );
  28.     $this->book_id = $id;
  29.     $this->title = $row['title'];
  30.     $this->author = $row['author'];
  31.     $this->publisher = $row['publisher'];
  32.   }
  33.   function insert()
  34.   {
  35.     global $db;
  36.     $sth = $db->prepare(
  37. 'INSERT INTO book ( book_id, title, author, publisher )
  38.     VALUES ( 0, ?, ?, ? )'
  39.     );
  40.     $db->execute( $sth,
  41.       array( $this->title,
  42.         $this->author,
  43.         $this->publisher ) );
  44.     $res = $db->query( "SELECT last_insert_id()" );
  45.     $res->fetchInto( $row );
  46.     return $row[0];
  47.   }
  48.   function update()
  49.   {
  50.     global $db;
  51.     $sth = $db->prepare(
  52. 'UPDATE book SET title=?, author=?, publisher=?
  53.    WHERE book_id=?'
  54.     );
  55.     $db->execute( $sth,
  56.       array( $this->title,
  57.         $this->author,
  58.         $this->publisher,
  59.         $this->book_id ) );
  60.   }
  61.   function delete()
  62.   {
  63.     global $db;
  64.     $sth = $db->prepare(
  65.       'DELETE FROM book WHERE book_id=?'
  66.     );
  67.     $db->execute( $sth,
  68.       array( $this->book_id ) );
  69.   }
  70.   function delete_all()
  71.   {
  72.     global $db;
  73.     $sth = $db->prepare( 'DELETE FROM book' );
  74.     $db->execute( $sth );
  75.   }
  76. }
  77. $book = new Book();
  78. $book->delete_all();
  79. $book->set_title( "PHP Hacks" );
  80. $book->set_author( "Jack Herrington" );
  81. $book->set_publisher( "O'Reilly" );
  82. $id = $book->insert();
  83. echo ( "New book id = $id\n" );
  84. $book2 = new Book();
  85. $book2->load( $id );
  86. echo( "Title = ".$book2->get_title()."\n" );
  87. $book2->delete( );
  88. ?>
复制代码
为了保持代码简单,我把类和测试代码放在一个文件中。文件首先得到数据库句柄,句柄保存在一个全局变量中。然后定义 Book 类,用私有成员变量代表每个字段。还包含了一套用来从数据库装入、插入、更新和删除行的方法。

底部的测试代码先删除数据库中的所有条目。然后,代码插入一本书,输出新记录的 ID。然后,代码把这本书装入另一个对象并输出书名。

清单 3 显示了在命令行上用 PHP 解释器运行代码的效果。


清单 3. 在命令行运行代码
  1. % php db1.php
  2. New book id = 25
  3. Title = PHP Hacks
  4. %
复制代码
不需要看太多,就已经得到重点了。Book 对象代表图书数据表中的行。通过使用上面的字段和方法,可以创建新行、更新行和删除行。

梭哈游戏规则518.dapai666.com★★真钱游戏★★






金帝棋牌:http://518.dapai666.com  

      金帝真人真钱游戏总部设在英国,持有欧亚洲地区网络游戏竞技牌照,多年游戏平台的一流信誉保证, 采用最先进的防作弊游戏系统,系统更安全!   真钱斗地主,扎金花,梭哈,斗牛游戏是真正公平、公正的对抗游戏, 更具有刺激性和挑战性!  最低充值5元,充值24小时在线,兑换3小时到账。

现大优惠注册送10元即可免费赢大钱,赢了立即兑换,
一次性充值100元将获得5元现金奖励累计!娱乐赚钱两不误!  绝对刺激!  是中小型游戏


玩家的赢钱天堂!

每日精彩活动,奖励天天送,千元大奖等您拿:
     

积分奖励:月,周,日积分奖励,前10名都有奖,千元大奖等您拿
     

抽奖活动:每个玩家当天在游戏中玩够50局即可参加抽奖,100%中奖,最高奖励千元
在线奖励:每个玩家当天在线时间分别达到1小时奖1元,3小时奖5元,6小时奖15元,12小时奖50元
比赛奖励:每天晚上19点-22点举行斗地主,扎金花,梭哈比赛,前10名都有奖!   

时 时 彩:2元买一注,奖金1元-1万元不等。
充值奖励:一次性充值至少100元奖励5元,依次类推..

                                                                  


只要你有本事,一天赚1000不是问题!

赢了拿走,还等什么呢 !

      咨询QQ:1416960583




TOP

返回列表