从PHP检索数据库中的数据

时间:2012-05-22 05:35:41

标签: php mysql

在用户点击上一页上的书名后,尝试从关系表中检索相关数据。即使在数据库中存在数据,页面上也不会打印任何内容。

表架构是:

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>

2 个答案:

答案 0 :(得分: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>";
}

答案 1 :(得分:0)

我的建议:

a)请改变SQ​​L语句中的变量,如下所示:

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

b)确保您的变量$sTitle不为空。如果仍然存在问题,请回显整个SQL语句(echo $strSQL;)以进行进一步调试。

c)在此期间,请改革整个SQL语句。以下语句执行相同操作并且更具可读性:

$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)而不是在if($row = mysql_fetch_array($rs)){之后输出数据,而是使用while语句,例如

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

e)问题: 表格书INNER JOIN是否有特定原因,但在条件中没有使用?