连接数据库 - 脚本建议?

时间:2011-01-09 16:21:18

标签: php mysql oop

<html><head><title>Library Books</title></head>
<body>
<table border=1>
<tr><th>Book</th><th>Year Published</th><th>Author</th></tr>
<?php
 // connect
 require_once('DB.php');
 $db = DB::connect("mysql://librarian:passw0rd@localhost/library");
 if (DB::iserror($db)) {
   die($db->getMessage(  ));
 }
 // issue the query
 $sql = "SELECT books.title,books.pub_year,authors.name
         FROM books, authors
         WHERE books.authorid=authors.authorid
         ORDER BY books.pub_year ASC";
 $q = $db->query($sql);
 if (DB::iserror($q)) {
   die($q->getMessage(  ));
 }
 // generate the table
 while ($q->fetchInto($row)) {
?>
<tr><td><?= $row[0] ?></td>
    <td><?= $row[1] ?></td>
    <td><?= $row[2] ?></td>
</tr>
<?php
 }
?>

DB.php应如何使脚本运行?

这不起作用:

<?php
define("DB_SERVER", "localhost");
define("DB_NAME", "***");
define ("DB_USER", "***");
define ("DB_PASSWORD", "***");
?>

提前致谢

1 个答案:

答案 0 :(得分:0)

您必须实现DB类以及脚本调用的所有方法。根据您粘贴的内容,connectisErrorgetMessagequeryfetchInto。你真的不想编写自己的DB包装器。

实际上看起来代码正在尝试使用旧的PEAR DB package

查看MDB2包。非常好。