2011-05-07 44 views
1

我想創建一個基於2 MYSQL表中存儲的內容的用戶評級系統。基於來自2個表的MYSQL結果顯示特定的img。 SELECT,PHP

一張桌子被稱爲「imageMarkkers」,和其他「標記」。

我想檢查行「authorid」,這兩個表中都是相同的,它存儲基於用戶的'authorid'的數字。

我想顯示一個特定的圖像,基於特定用戶ID號碼在「authorid」中找到的來自表格「標記」和「圖像標記」的次數。

這個聲明在頁面的頂部:

$theID = $_GET['id']; 

代碼中,我至今是:

<? $img0 = "<img src='../webimages/0star.png'>"; 
    $img1 = "<img src='../webimages/1star.png'>"; 
    $img2 = "<img src='../webimages/2star.png'>"; 
    $img3 = "<img src='../webimages/3star.png'>"; 
    $img4 = "<img src='../webimages/4star.png'>"; 
    $img5 = "<img src='../webimages/5star.png'>"; ?> 

    <? $result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = $theID"); 
$rows = mysql_num_rows($result3); ?> 

<?php 
while($row = mysql_fetch_array($result3)){ 

if ($result3 > 1) { 
    echo "$img1"; 
} elseif ($result3 == 5) { 
    echo "$img2"; 
} elseif ($result3 == 10) { 
    echo "$img3"; 
} elseif ($result3 == 20) { 
    echo "$img4"; 
} elseif ($result3 == 30) { 
    echo "$img5"; 
} else { 
    echo "$img0"; 
} 
?> 

<? } ?> 

我的MySQL錶行 「AUTHORID」 存儲爲INT,11,並允許null勾選。

錯誤即時得到的是:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource.... 

請幫我解決這個問題。 非常感謝您的時間。

更新,這是新的代碼我有,並且仍然顯示「$ IMG 0」,當它應該顯示「$ IMG3」:

<? $img0 = "<img src='../webimages/0star.png'>"; 
$img1 = "<img src='../webimages/1star.png'>"; 
$img2 = "<img src='../webimages/2star.png'>"; 
$img3 = "<img src='../webimages/3star.png'>"; 
$img4 = "<img src='../webimages/4star.png'>"; 
$img5 = "<img src='../webimages/5star.png'>"; ?> 

<? $row[0] = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '". (int)$theID."'"); 

    if ($row[0] > 1) { 
     echo "$img1"; 
    } elseif ($row[0] > 5) { 
     echo "$img2"; 
    } elseif ($row[0] > 10) { 
     echo "$img3"; 
    } elseif ($row[0] > 20) { 
     echo "$img4"; 
    } elseif ($row[0] > 30) { 
     echo "$img5"; 
    } else { 
     echo "$img0"; 
    } 
    ?> 

我指望其中,具體的用戶數顯示並在'authorid'行中匹配'theID',來自'tablemarkamers'和'markers'。用戶正在將其他數據存儲在數據庫中,並且通過存儲這些數據將'authorid'行填入「標記」和「圖像標記」中,根據它們填入的表單來存儲信息。

我希望這有助於你明白我試圖做...

回答

0

添加單引號括起來$theID,也確保$ theID檢查在它前面的整數或地方(INT):

$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '".(int)$theID."'"); 

如果錯誤仍然發生這種替換,看看你會得到什麼樣的錯誤:

$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid = '".(int)$theID."'") or die(mysql_error()); 

因爲那時$result3沒有有效的結果resou RCE爲mysql_num_rows

+0

太棒了!謝謝,第一條select語句起作用了。它顯示的圖像,但它顯示'$ img0',當用戶'authorid'出現14次'標記'和'imageMarkkers',所以應該顯示'$ img5',所以我的IF語句必須有錯誤... – user736338 2011-05-07 17:04:48

+1

您的查詢尚未優化。自從最初的問題得到解決後,在這裏更好地創建一個新問題你必須問自己(並告訴我們)爲什麼你使用2個表格而不是1個,他們的關係/結構是什麼。 – 2011-05-07 17:34:06

0
<? 
$result3 = mysql_query("SELECT * FROM `markers`, `imagemarkers` WHERE authorid=".(int)$theID.")"; 
$rows = mysql_num_rows($result3); 
?> 
0

首先,如果行「AUTHORID」這兩個表中存在,應該在查詢中,你指的是哪一個規定:

$theID = intval($_GET['id']); // to avoid SQL-injections 
$result3 = mysql_query("SELECT * FROM `markers` m, `imagemarkers` im WHERE m.authorid = $theID AND im.authorid = $theID"); 
// m and im are just table-aliases for brevity, you can use whatever names you like 

其次,替換$ result3與$ row [0] inside「while」循環:

if ($row[0] > 1) { ... 

最後,邏輯出現了問題。

你能解釋一下完整的DB結構嗎,除了authorid外還有哪些列?

+0

在這種情況下'm.authorid = im.authorid AND m.authorid ='「。(int)$ theID。」'''是beter要使用的。這將連接兩個表。 – 2011-05-07 17:00:47

+0

是的,那可能會更好。問題是我不知道發生了什麼,爲什麼需要兩張表;-)需要主題啓動器的更多細節。附:爲什麼'(int)'有'intval()'的時候? – 2011-05-07 17:03:57

+0

一定錯過了intval ;-) – 2011-05-07 17:05:01