php connect แบบ pdo พอจะเปลี่ยน DB มีข้อจำกัดในเรื่อง Syntax ครับ
มันก็ไม่ต่างกันมากนักหรอก
แต่ยังไงก็ต้องเปลี่ยนต้อง native command อยู่ดี เช่น limit (mysql) กับ top (sql server)
แต่ถ้าเป็น standard command ก็ไม่ต้องเปลี่ยน
Code (PHP)
// Microsoft Sql Server Express
define('SQLSRV_HOST', 'localhost\sqlexpress');
define('SQLSRV_NAME', 'northwind');
define('SQLSRV_USER', '');
define('SQLSRV_PASSWORD', '');
// MySql Server
define('MYSQL_HOST', 'localhost');
define('MYSQL_NAME', 'northwind');
define('MYSQL_USER', 'root');
define('MYSQL_PASSWORD', 'mysql56');
mysql
<?php
$dns = "mysql:host=" . MYSQL_HOST . ";dbname=" . MYSQL_NAME . ";";
$dbh = new PDO($dns, MYSQL_USER, MYSQL_PASSWORD);
$query = 'SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, Address, City, Country FROM Employees;';
$stmt = $dbh->prepare($query);
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo "<td><img src=\"images/imageicon.png\"
onmouseover=\"javascript:showtrail('imagedb.php?db=mysql&id={$result['EmployeeID']}');\"
onmouseout=\"javascript:hidetrail();\" style=\"border-width:0px;\" /></td>";
echo "<td>{$result['TitleOfCourtesy']} {$result['LastName']} {$result['FirstName']}</td>";
echo "<td>{$result['Title']}</td>";
echo "<td>" . (new DateTime($result['BirthDate']))->format('d M Y') . "</td>";
echo "<td>{$result['Address']}</td>";
echo "<td>{$result['City']}</td>";
echo "<td>{$result['Country']}</td>";
echo "</tr>\n";
}
?>
sql server
<?php
$dns = "sqlsrv:Server=". SQLSRV_HOST .";Database=" . SQLSRV_NAME . ";";
$dbh = new PDO($dns, SQLSRV_USER, SQLSRV_PASSWORD);
$query = 'SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, Address, City, Country FROM Employees;';
$stmt = $dbh->prepare($query);
$stmt->execute();
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo "<td><img src=\"images/imageicon.png\"
onmouseover=\"javascript:showtrail('imagedb.php?db=sqlsrv&id={$result['EmployeeID']}');\"
onmouseout=\"javascript:hidetrail();\" style=\"border-width:0px;\" /></td>";
echo "<td>{$result['TitleOfCourtesy']} {$result['LastName']} {$result['FirstName']}</td>";
echo "<td>{$result['Title']}</td>";
echo "<td>" . (new DateTime($result['BirthDate']))->format('d M Y') . "</td>";
echo "<td>{$result['Address']}</td>";
echo "<td>{$result['City']}</td>";
echo "<td>{$result['Country']}</td>";
echo "</tr>\n";
}
?>
Date :
2013-11-11 15:31:06
By :
ห้ามตอบเกินวันละ 2 กระทู้
ขอบคุณครับ
Date :
2013-11-11 16:23:24
By :
poomin
ตัวอย่างการเขียน SQL Server กับ PHP ผ่าน PDO
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & Windows Azure (SQL Azure)</title>
</head>
<body>
<?
$serverName = "tcp:bc6hela9fr.database.windows.net,1433";
$userName = 'thaicreate-user@bc6hela9fr';
$userPassword = 'password@123';
$dbName = "thaicreate-db";
$conn = new PDO("sqlsrv:server=$serverName ; Database = $dbName", $userName, $userPassword);
$strSQL = "SELECT * FROM customer";
$stmt = $conn->prepare($strSQL);
$stmt->execute();
?>
<table width="600" 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>
</tr>
<?
$arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($arrValues as $result){
?>
<tr>
<td><div align="center"><?=$result["CustomerID"];?></div></td>
<td><?=$result["Name"];?></td>
<td><?=$result["Email"];?></td>
<td><div align="center"><?=$result["CountryCode"];?></div></td>
<td align="right"><?=$result["Budget"];?></td>
<td align="right"><?=$result["Used"];?></td>
</tr>
<?
}
?>
</table>
<?
$conn = null;
?>
</body>
</html>
การใช้งาน SQL Azure ร่วมกับ PHP (SQL Server) (Odbc/sqlsrv/PDO)
Date :
2013-11-12 21:16:27
By :
mr.win
Load balance : Server 00