2013-10-08 36 views
0

最初我的問題是我的帖子是重複的。我相信這是因爲我沒有在SQL語句中鏈接我的表。現在我已經完成了,我在我的if info.eof語句之後打印了「找不到數據」。爲什麼ASP沒有在我的數據庫中找到任何東西?

我確定數據庫中有信息,因爲我手動檢查過,但沒有成功。

這裏是我的代碼:

<% option explicit %> 
<!DOCTYPE html> 
<html> 
<head> 

    <link rel="stylesheet" type="text/css" href="normalize.css"> 
</head> 

<body> 
<!--#include file="header.asp"--> 
<!--#include file="dbconn.asp"--> 



<% 


    dim sql, info 

'        0     1  2  3   4    5    
SQL = "select projectName, UserName, Category, Created, url, Projectstable.description "&_ 
     "from ProjectsTable, PictureTable, Usertable, CategoryTable "&_ 
     "where Projectstable.ID=PictureTable.projectNum AND "&_ 
     "categoryNum=CategoryTable.ID AND "&_ 
     "ProjectsTable.userNum=Usertable.ID AND "&_ 
     "UserTable.ID=PictureTable.userNum "&_ 
      "order by Created" 

set info=conn.execute(SQL) 

    if info.eof then 
    response.write "<p>No Data Found</p>" 
    else 

    do 
     response.write "<h1>" & info(0) & "</h1> By: " & info(1) & " Posted in: " & info(2) & " Created on: " & info(3) & "<br>" &_ 
        "<img src=""images/" &info(4)&""">" &_ 
        "<p>" & info(5) & "</p>" 

     info.movenext 
    loop until info.eof 
    end if 

    conn.close 

%> 

</body> 

</html> 
+0

您是否試圖提取查詢並直接對數據庫運行它(如果您使用的是SQL Server,則在管理工作室中運行)。它顯示結果嗎? – mortb

+0

@mortb你不得不原諒我,因爲我不明白你的建議。我的數據庫在Microsoft Access上。 – chap

+0

問題可能出在dbconn.asp – John

回答

-1

爲什麼下劃線出現在你的代碼?或第一「'」 清潔你的代碼我得到:

<% option explicit %> 
<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="normalize.css"> 
</head> 
<body> 
<!--#include file="header.asp"--> 
<!--#include file="dbconn.asp"--> 
<% 
    dim sql, info 
SQL = "select projectName, UserName, Category, Created, url, Projectstable.description "& 
     "from ProjectsTable, PictureTable, Usertable, CategoryTable "& 
     "where Projectstable.ID=PictureTable.projectNum AND "& 
     "categoryNum=CategoryTable.ID AND "& 
     "ProjectsTable.userNum=Usertable.ID AND "& 
     "UserTable.ID=PictureTable.userNum "& 
      "order by Created" 
set info=conn.execute(SQL) 
    if info.eof then 
    response.write "<p>No Data Found</p>" 
    else 
    do 
     response.write "<h1>" & info(0) & "</h1> By: " & info(1) & " Posted in: " & info(2) & " Created on: " & info(3) & "<br>" & 
        "<img src=""images/" &info(4)&""">" & 
        "<p>" & info(5) & "</p>" 
     info.movenext 
    loop until info.eof 
    end if 
    conn.close 

%> 
</body> 
</html> 
+0

這只是意味着繼續在一個新的線上的聲明。 – chap

+0

@Kentoro:不要將問題發佈爲答案。你在評論中做的。 – mortb

1

我已經重寫使用ANSI 92顯式連接語法,而不是你已經使用了ANSI 89版本(如名稱被替換過20查詢年前明確連接):

SELECT projectName, UserName, Category, Created, url, Projectstable.description 
FROM ProjectsTable 
     INNER JOIN PictureTable 
      ON Projectstable.ID = PictureTable.projectNum 
     INNER JOIN Usertable 
      ON UserTable.ID = PictureTable.userNum 
      AND ProjectsTable.userNum = Usertable.ID 
     INNER JOIN CategoryTable 
      ON categoryNum = CategoryTable.ID 
ORDER BY Created; 

其中強調,我認爲你是限制你的結果中的圖片表格的用戶是一樣的,在項目表中的用戶。這是故意的嗎?

如果您刪除了其中一個標準,您是否獲得了所需的結果?因此,無論

SELECT projectName, UserName, Category, Created, url, Projectstable.description 
FROM ProjectsTable 
     INNER JOIN PictureTable 
      ON Projectstable.ID = PictureTable.projectNum 
     INNER JOIN Usertable 
      ON UserTable.ID = PictureTable.userNum 
      --AND ProjectsTable.userNum = Usertable.ID 
     INNER JOIN CategoryTable 
      ON categoryNum = CategoryTable.ID 
ORDER BY Created; 

或者

SELECT projectName, UserName, Category, Created, url, Projectstable.description 
FROM ProjectsTable 
     INNER JOIN PictureTable 
      ON Projectstable.ID = PictureTable.projectNum 
     INNER JOIN Usertable 
      ON ProjectsTable.userNum = Usertable.ID 
      --AND UserTable.ID = PictureTable.userNum 
     INNER JOIN CategoryTable 
      ON categoryNum = CategoryTable.ID 
ORDER BY Created; 

有關ANSI 89進一步雷丁VS ANSI 92聯接語法Aaron Bertranda great article about it在他的壞習慣踢系列。

+1

我的講師很老派,所以不會讓我感到驚訝。當我離開時,我仍然遇到同樣的問題。 我想我想讓用戶一樣,但我不確定。 – chap

1

答案實際上是數據庫本身的錯誤。更有意思的是,表格之間的鏈接不正確。 SQL語句可能已過時,但不會導致錯誤。

相關問題