2016-12-06 56 views
1

我必須讓這個代碼返回相同的結果,但只有在用戶ID是一個數字(例如16); 每個設備在此表中都有一個用戶標識和一個用戶名。 這是代碼:PHP&MySQL,獲取表

<?php 

$myQuery= 'SELECT * FROM devicesissued di LEFT JOIN usercred uc ON di.userID = uc.userID WHERE 1 ORDER BY timeStamp DESC'; 
$result = mysql_query($myQuery) or die($myQuery."<br/><br/>".mysql_error()); 
while($row = mysql_fetch_array($result)) 
{ 
    $catQuery = 'SELECT * FROM deviceinventory WHERE deviceID = '. $row['deviceID']; 
    $catResult = mysql_query($catQuery) or die($catQuery."<br/><br/>".mysql_error()); 
    $cat = mysql_fetch_array($catResult); 
    echo "<tr>"; 
    echo "<td>" . $row['displayName'] . "</td>"; 
    echo "<td>" . $row['deviceID'] . "</td>"; 
    echo "<td>" . $cat['barCodeID'] . "</td>"; 
    echo "<td>" . $cat['category'] . "</td>"; 
    echo "<td>" . $row['deviceName'] . "</td>"; 
    echo "<td>" . $cat['operatingSystem'] . "</td>"; 
    echo "<td>" . $cat['serialNumber'] . "</td>"; 
    echo "<td>" . $cat['macAddress'] . "</td>"; 
    echo "<td>" . $row['timeStamp'] . "</td>"; 
    echo "</tr>"; 
} 

?> 

如果我添加了WHERE 'userID' = A-Number條款它不會做任何事情。

如果您有任何想法,我會很樂意嘗試。

謝謝。

+6

'mysql' extension ='bad idea' ... – Hackerman

+1

'WHERE 1 ORDER'why'1' here? – Thamaraiselvam

+0

你真的用引號加'WHERE'userID'嗎?這意味着您將字符串與ID進行比較,而不是列值。查詢應該是'SELECT * FROM devicesissued di LEFT JOIN usercred uc ON di.userID = uc.userID WHERE uc.userId = 16 ORDER BY timeStamp DESC' – Finwe

回答

1

試圖改變自己的查詢有點

$myQuery= 'SELECT * FROM devicesissued di 
      LEFT JOIN usercred uc ON di.userID = uc.userID 
      WHERE di.userID=the_number_here ORDER BY timeStamp DESC'; 

當你有兩個連接表(deviceissuedusercred)的用戶ID,你要告訴MySQL上表中,您要執行的WHERE -clause 。

+0

非常感謝,完美工作:D –

+0

不客氣:) – Andreas