加入3个表来显示PHP-MSSQL的某些数据

时间:2013-01-31 03:16:28

标签: php sql sql-server database join

所以我有这些表格,我希望获得某些数据供用户查看并能够发布到其他页面

我无法发布图片所以我必须将其分解,所以请耐心等待我

第一桌

  • dbo.users
  • p键(用户ID)
  • EmployeeName

第二桌

  • dbo.PC
  • p键(PCID)
  • PC_Number

第3桌

  • dbo.FA_PC
  • p键(FAID)
  • FKEY(用户ID)
  • FKEY(PCID)

如何以相同的形式显示当前所选的$ rs->字段('UserID')的PC_Number,并且仍然可以在printd.php上发布

我不知道如何连接dbo.users-> dbo.FA_PC-> dbo.PC

<form action="printd.php" method="post" target="_blank">
<?php
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
   try {
   $myServer = "WTCPHFILESRV\WTCPHINV";
   $myUser = "sa";
   $myPass = "P@ssw0rd";
   $myDB = "wtcphitinventory";   
   $connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
   $conn->open($connStr); 
         if (! $conn) {
            throw new Exception("Could not connect!");
        }
   }
   catch (Exception $e) {
      echo "Error (File:): ".$e->getMessage()."<br>";
   }

if (!$conn)
  {exit("Connection Failed: " . $conn);}

    $sql_exp = "select * from dbo.users"; 

  $rs = $conn->Execute($sql_exp);
echo "<select name='empt'>";
   while (!$rs->EOF) {
       set_time_limit(0);
        echo "<option value=".$rs->Fields('UserID')." >".$rs->Fields('EmployeeName')."</option>";

      $rs->MoveNext();
   }       
   $rs->Close();   



       ?>

1 个答案:

答案 0 :(得分:2)

联接看起来像这样,

SELECT  a.*, c.PC_Number
FROM    users a
        INNER JOIN FA_PC b
            ON a.UserID = b.UserID
        INNER JOIN PC c
            ON b.PCID = c.PCID

要充分了解联接知识,请访问以下链接: