http://bbs.phppx.com/thread-29-1-1.html

http://bbs.phppx.com/thread-29-1-1.html

 

先从一个简单的数据库模式开始。清单 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 对象代表图书数据表中的行。通过使用上面的字段和方法,可以创建新行、更新行和删除行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值