PHP SQL Server Get Display Data Master-Detail (sqlsrv)       | 
   
 
			  
			  
                PHP SQL Server Get Display Data Master-Detail (sqlsrv) บทความนี้จะเป็นตัวอย่างของ sqlsrv  การเขียน PHP เพื่ออ่านข้อมูลจาก Database ของ SQL Server มาแสดงผลทางหน้าจอในรูปแบบของ Master-Detail ด้วย function ต่าง ๆ ของ sqlsrv ทำงานร่วมกับการ fetch ข้อมูลในรูปแบบของ array 
 
Syntax รูปแบบการใช้งาน 
$strCustomerID = "C001";
$stmt = "SELECT * FROM customer WHERE CustomerID = ? ";
$params = array($strCustomerID);
$query = sqlsrv_query( $conn, $stmt, $params);
$result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
if($result)
{
	echo $result["CustomerID"]; 
}
 
 
  
 
ฐานข้อมูลและตารางของ SQL Server Database 
 
 
 
Example ตัวอย่างการเขียน PHP กับ SQL Server เพื่อแสดงรายการข้อมูลแบบ Master-Detail 
 
list.php 
<html>
<head>
<title>ThaiCreate.Com PHP & SQL Server (sqlsrv)</title>
</head>
<body>
<?php
	ini_set('display_errors', 1);
	error_reporting(~0);
	$serverName = "localhost";
	$userName = "sa";
	$userPassword = "";
	$dbName = "mydatabase";
	$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
	$conn = sqlsrv_connect( $serverName, $connectionInfo);
	if( $conn === false ) {
		die( print_r( sqlsrv_errors(), true));
	}
	$stmt = "SELECT * FROM customer";
	$query = sqlsrv_query($conn, $stmt);
?>
<table width="650" border="1">
  <tr>
    <th width="91"> <div align="center">CustomerID </div></th>
    <th width="98"> <div align="center">Name </div></th>
    <th width="198"> <div align="center">Email </div></th>
    <th width="97"> <div align="center">CountryCode </div></th>
    <th width="59"> <div align="center">Budget </div></th>
    <th width="71"> <div align="center">Used </div></th>
	<th width="50"> <div align="center">Detail </div></th>
  </tr>
<?php
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
?>
  <tr>
    <td><div align="center"><?php echo $result["CustomerID"];?></div></td>
    <td><?php echo $result["Name"];?></td>
    <td><?php echo $result["Email"];?></td>
    <td><div align="center"><?php echo $result["CountryCode"];?></div></td>
    <td align="right"><?php echo $result["Budget"];?></td>
    <td align="right"><?php echo $result["Used"];?></td>
	<td align="center"><a href="detail.php?CustomerID=<?php echo $result["CustomerID"];?>">Detail</a></td>
  </tr>
<?php
}
?>
</table>
<?php
sqlsrv_close($conn);
?>
</body>
</html>
 
 
detail.php 
<html>
<head>
<title>ThaiCreate.Com PHP & SQL Server (sqlsrv)</title>
</head>
<body>
<?php
	ini_set('display_errors', 1);
	error_reporting(~0);
	$serverName = "localhost";
	$userName = "sa";
	$userPassword = "";
	$dbName = "mydatabase";
	$strCustomerID = null;
	if(isset($_GET["CustomerID"]))
	{
		$strCustomerID = $_GET["CustomerID"];
	}
	$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
	$conn = sqlsrv_connect( $serverName, $connectionInfo);
	if( $conn === false ) {
		die( print_r( sqlsrv_errors(), true));
	}
	$stmt = "SELECT * FROM customer WHERE CustomerID = ? ";
	$params = array($strCustomerID);
	$query = sqlsrv_query( $conn, $stmt, $params);
	$result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
?>
<table width="284" border="1">
  <tr>
    <th width="120">CustomerID</th>
    <td width="238"><?php echo $result["CustomerID"];?></td>
    </tr>
  <tr>
    <th width="120">Name</th>
    <td><?php echo $result["Name"];?></td>
    </tr>
  <tr>
    <th width="120">Email</th>
    <td><?php echo $result["Email"];?></td>
    </tr>
  <tr>
    <th width="120">CountryCode</th>
    <td><?php echo $result["CountryCode"];?></td>
    </tr>
  <tr>
    <th width="120">Budget</th>
    <td><?php echo $result["Budget"];?></td>
    </tr>
  <tr>
    <th width="120">Used</th>
    <td><?php echo $result["Used"];?></td>
    </tr>
  </table>
<?php
sqlsrv_close($conn);
?>
</body>
</html>
 
 
Screenshot 
 
  
 
แสดงข้อมูลจาก SQL Server ด้วย function ของ sqlsrv ซึ่งสามารถคลิกดู Detail ได้ 
 
  
 
แสดง Master-Detail ของรายการทีคลิก 
 
              
  
              			
			  
								  
			  
  
                          
  |