2011-01-09 58 views
0
<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:[email protected]/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", "***"); 
?> 

在此先感謝

+0

你會得到什麼錯誤? – 2011-01-09 16:22:06

+0

致命錯誤:在第8行的xxx.php中找不到'DB'類 – easyrider 2011-01-09 16:32:57

回答

0

你將不得不實行DB類和所有的腳本調用該方法。從你粘貼的內容看,connect,isError,getMessage,queryfetchInto。你真的不想編寫你自己的數據包封裝器。

它看起來像代碼試圖使用舊的PEAR DB package。請參考MDB2包。這很不錯。