2012-02-19 153 views
0

我正在網站上進行教學使用,當我嘗試構建一個執行某些邏輯的文件時,我遇到了一個小錯誤。MySQL資源錯誤?

基本上,代碼是爲了獲得3個數據值,如果只有數據的第一位存在,它運行x查詢,如果第一個和第二個存在它運行y查詢,依此類推等等,直到它對GET數據運行8次測試。對於一些背景信息,水平=一類學習水平(IGCSE,IB,A-Levels),學科是一門學科(數學,英語,科學),學校是學校的名稱。

數據庫結構如下:

'id' INT(11) 
'title' VARCHAR(255) 
'description' VARCHAR(1000) 
'filename' VARCHAR(255) 
'uploader' VARCHAR(255) 
'school' VARCHAR(255) 
'subject' VARCHAR(255) 
'level' VARCHAR(255) 
'downloads' VARCHAR(20) 
'views' VARCHAR(20) 
'helpful' INT(11) 
'nothelpful' INT(11) 
'date' DATE 

而且我運行下面的PHP代碼:

<? 
/* 
YYY 
YNY 
YNN 
YYN 
NNN 
NYN 
NNY 
NYY 
*/ 
/* DECLARE VARS */ 
$subject =   $_GET['subject']; 
$level  =   $_GET['lvl']; 
$school  =   $_GET['school']; 
include("connect.php"); 

if($subject!="" && $level!="" && $school!=""){ 
/* GET Notes that have x subject, y level, and z school */ 
$yyyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' AND school='$school' ORDER BY helpful DESC LIMIT 30"); /* quote missed here */ 
while($row=mysql_fetch_array($yyyq)){ 
echo '<span id="topic">[<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['level'].' '.$row['subject'].'</a>]</span> <a href="note.php?id='.$row['id'].'">'.$row['title'].'</a> <span id="usersize"><i>[uploaded by <a href="profile.php?id='.$row['uploader'].'">'.$row['uploader'].'</a>]</i></span><br>'; } 
//yyy 
} 

if($subject!="" && $level!="" && $school==""){ 
/* Get Notes from x Subject and X Level, regardless of school. */ 
$yynq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' ORDER BY helpful DESC LIMIT 30"); /* quote missed here */ 
while($row=mysql_fetch_array($yynq)){ 
echo '<span id="topic">[<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['level'].' '.$row['subject'].'</a>]</span> <a href="note.php?id='.$row['id'].'">'.$row['title'].'</a> <span id="usersize"><i>[uploaded by <a href="profile.php?id='.$row['uploader'].'">'.$row['uploader'].'</a>]</i></span><br>'; } 
//yyn 
} 

if($subject!="" && $level=="" && $school==""){ 
/* Get Level before Getting Notes without School */ 
$ynnq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' GROUP BY level"); 
echo "<center>"; 
while($row=mysql_fetch_array($ynnq)){ 
echo '<a href="list.php?lvl='.$row['level'].'&subject='.$row['subject'].'">'.$row['level'].'</a><br>'; } 
//ynn  
} 

if($subject!="" && $level=="" && $school!=""){ 
/* Get level based on school + subject */ 
$ynyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND school='$school' GROUP BY level"); 
while($row=mysql_fetch_array($ynyq)){ 
echo '<a href="list.php?lvl='.$row['level'].'&school='.$row['school'].'&subject='.$row['subject'].'">'.$row['level'].'</a><br>'; } 
//yny 
} 

if($subject=="" && $level=="" && $school==""){ 
//Error - Nothing + Nothing + Nothing = Nothing 
//nnn 
} 

if($subject=="" && $level=="" && $school!=""){ 
/* Get Subject First based on School, thus redirecting to previous ynn (reverse clause)*/ 
$nnyq = mysql_query("SELECT * FROM noteshare_notes WHERE school='$school' GROUP BY subject"); 
while($row=mysql_fetch_array($nnyq)){ 
echo '<a href="list.php?subject='.$row['subject'].'&school='.$row['school'].'">'.$row['subject'].'</a><br>'; } 
//nny 
} 

if($subject=="" && $level!="" && $school!=""){ 
/* Get Subjects based on level + school */ 
$nyyq = mysql_query("SELECT * FROM noteshare_notes WHERE level='$level' AND school='$school' GROUP BY subject"); 
while($row=mysql_fetch_array($nyyq)){ 
echo '<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'&school='.$row['school'].'">'.$row['subject'].'</a><br>'; } 
//nyy 
} 

if($subject=="" && $level!="" && $school==""){ 
/* Get Subject based on level */ 
$nynq = mysql_query("SELECT * FROM noteshare_notes WHERE level='$level' GROUP BY subject"); 
while($row=mysql_fetch_array($nyyq)){ 
echo '<a href="list.php?subject='.$row['subject'].'&lvl='.$row['level'].'">'.$row['subject'].'</a><br>'; } 
//nyn 
} 
?> 

道歉,因爲它的意見和概率積累,哈哈。

它讓我回國與

mysql_fetch_array(): supplied argument is not a valid MySQL result resource 

錯誤,我不知道如何解決它,我試過很多,很多的事情來解決,沒有什麼工作。我認爲這可能是由於多個AND或數據庫中沒有30行的事實

有人會知道如何解決這個問題嗎?任何答案將是非常有用的:)

乾杯!

+0

哪種情況會引發錯誤?或者所有這些都會導致錯誤? – Chris 2012-02-19 12:18:08

+0

把下面的代碼放在你的'mysql_query',plz:'if(mysql_errno()){echo mysql_error(); }併發布它生成的輸出。 – Sirko 2012-02-19 12:18:12

+0

沒有錯誤顯示@Sirko :(整個頁面死亡 – unicornication 2012-02-19 12:29:30

回答

1

給這2個試一試。你在陌生的地方有單引號。在$學校之後你沒有單獨的報價,並且在這兩種情況下在LIMIT 30之後都有一個單引號。這可能會導致MySQL錯誤,或至少導致意外的行被選中。

$yyyq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' AND school='$school' ORDER BY helpful DESC LIMIT 30"); 

$yynq = mysql_query("SELECT * FROM noteshare_notes WHERE subject='$subject' AND level='$level' ORDER BY helpful DESC LIMIT 30"); 
+0

啊,乾杯西蒙 - 這又一個小小的改正似乎這樣做: ) 非常非常感謝你! – unicornication 2012-02-19 12:53:46

+0

當它是一個簡單的答案時它總是很好誒;):) – 2012-02-19 13:00:18

+0

非常;)15char。雖然我認爲'xchar'反對SO TOS,所以,嗨:D – unicornication 2012-02-26 09:09:03