2012-05-22 241 views
0

嘗試從關係表中檢索相關數據,用戶單擊上一頁上的書名後。即使在數據庫中存在數據,也不會在頁面上打印任何內容。從PHP數據庫中檢索數據

表架構是:

relationshipID,bookone,booktwo,關係,relationlike,relationdislike

<html> 
    <head> 
    <title>Retrieve Relationships</title> 
    </head> 
    <body> 

    <dl> 

    <?php 
    // Connect to database server 
    mysql_connect("latcs7.cs.latrobe.edu.au","12ice06","EsnYkuxuwh9RbtQuRcQt") or die (mysql_error()); 

    // Select database 
    mysql_select_db("12ice06") or die(mysql_error()); 
     $sTitle=0; 
    // Get data from the database depending on the value of the id in the URL 
    $title = (isset($_GET['title']) && is_string($_GET['title'])) ? $_GET['title'] : null; 
$sTitle = mysql_real_escape_string($title); 
$strSQL = "SELECT R.bookone, B.title, B.author, 
     R.booktwo, B.title, B.author, 
     R.relation, R.relationlike, R.relationdislike 
     FROM relationships R 
     INNER JOIN books B ON R.bookone = B.bookid";  

$rs = mysql_query($strSQL) or die(mysql_error()); 
    // Loop the recordset $rs 

while($row = mysql_fetch_array($rs)){ 
    // Write the data of the book 


    echo "<dt>Book One:</dt><dd>" . $row["bookone"] . "</dd>"; 
    echo "<dt>Title:</dt><dd>" . $row["title"] . "</dd>"; 
    echo "<dt>Author:</dt><dd>" . $row["author"] . "</dd>"; 
    echo "<dt>Book Two:</dt><dd>" . $row["booktwo"] . "</dd>"; 
     echo "<dt>Title:</dt><dd>" . $row["title"] . "</dd>"; 
    echo "<dt>Author:</dt><dd>" . $row["author"] . "</dd>"; 
    echo "<dt>Relationship:</dt><dd>" . $row["relation"] . "</dd>"; 
    echo "<dt>Likes:</dt><dd>" . $row["relationshiplikes"] . "</dd>"; 
    echo "<dt>Dislikes:</dt><dd>" . $row["relationshipdislikes"] . "</dd>"; 
} 

echo $strSQL; 
    // Close the database connection 
    mysql_close(); 
    ?> 

    </dl> 
    <p><a href="search_bookl.php">Return to the list</a></p> 

    </body> 

    </html> 
+1

請停止使用古代mysql_ *函數編寫新代碼。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。相反,您應該瞭解準備好的語句並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)。如果你關心學習,[這裏是一個很好的PDO相關教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 – DCoder

+0

不幸的是它的語言,我們被告知用於這項任務。 – user1393064

+0

你是否直接在mysql中試過這個查詢?它是否返回任何行? –

回答

2
if($row = mysql_fetch_array($rs)){ 

    // Write the data of the book 
    echo "<dt>Book One:</dt><dd>" . $row["bookone"] . "</dd>"; 
    echo "<dt>Book Two:</dt><dd>" . $row["booktwo"] . "</dd>"; 
    echo "<dt>Relationship:</dt><dd>" . $row["relation"] . "</dd>"; 
    echo "<dt>Likes:</dt><dd>" . $row["relationshiplikes"] . "</dd>"; 
    echo "<dt>Dislikes:</dt><dd>" . $row["relationshipdislikes"] . "</dd>"; 
}while($row!=0); 

應該

while($row = mysql_fetch_array($rs)){ 
    // Write the data of the book 
    echo "<dt>Book One:</dt><dd>" . $row["bookone"] . "</dd>"; 
    echo "<dt>Book Two:</dt><dd>" . $row["booktwo"] . "</dd>"; 
    echo "<dt>Relationship:</dt><dd>" . $row["relation"] . "</dd>"; 
    echo "<dt>Likes:</dt><dd>" . $row["relationshiplikes"] . "</dd>"; 
    echo "<dt>Dislikes:</dt><dd>" . $row["relationshipdislikes"] . "</dd>"; 
} 
+0

沒有東西印到屏幕上 – user1393064

+0

檢查你的sql,你真的是指'books.bookid =關係。bookone AND relationships.bookone ='{$ sTitle}''? 'books.book_id'和'$ sTitle'?他們一樣嗎? – xdazz

+0

不,bookone是書桌的外鍵,它引用了書桌中的標題。 我試圖讓書名和作者打印與其他書籍的關係。 – user1393064

0

我的建議是:

a)請重新形成你的SQL語句像這樣的變量:)

$strSQL = "SELECT [...] 
      WHERE books.bookid=relationships.bookone 
       AND relationships.bookone='".$sTitle."'"; 

b確保您的變量$sTitle不是空的。如果仍然存在問題,請回顯整個SQL語句(echo $strSQL;)以進一步調試。

c)雖然在上面,但請重新構造整個SQL語句。下面的語句做同樣的,是更可讀:相反if($row = mysql_fetch_array($rs)){後輸出數據的

$strSQL = "SELECT R.bookone, R.booktwo, 
       R.relation, 
       R.relationlike, R.relationdislike 
      FROM relationships R 
      INNER JOIN books B ON R.bookone = B.bookid 
      WHERE R.bookone='".$sTitle."'"; 

d),使用while語句來代替,像

while ($row = mysql_fetch_array($rs)) { 
    // Write the data of the book 
    // Insert your echos here 
} 

五)問: 是否有特定的原因表格書是INNER JOIN ed,但在條件下沒有使用?

+0

這是調試器打印的內容 SELECT R.bookone,R.booktwo,R.relation,R.relationlike,R.relationdislike FROM relationships R INNER JOIN books B ON R.bookone = B.bookid WHERE R.bookone = '' – user1393064

+0

然後你的變量'$ sTitle'是空的。確保'$ _GET ['title']'確實可用。 – Bjoern