PHP& MySQL,获取表

时间:2016-12-06 16:52:59

标签: php mysql

我必须让这段代码返回相同的结果,但只有在userID是一个数字的地方(ex 16); 每个设备在此表中都有一个用户标识和用户名。 这是代码:

<?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条款,它就不会做任何事情。

如果您有任何想法,我将很乐意尝试。

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试稍微更改您的查询

query_values

由于您在两个连接表($myQuery= 'SELECT * FROM devicesissued di LEFT JOIN usercred uc ON di.userID = uc.userID WHERE di.userID=the_number_here ORDER BY timeStamp DESC'; deviceissued)中都有userID,因此您必须告诉MySQL您要在哪个表上执行usercred - 子句。