PHP SQL Server Prepared Statement / Store Procedure (sqlsrv) |
PHP SQL Server Prepared Statement / Store Procedure (sqlsrv) บทความนี้จะเป็นการสรุปคำสั่งและรูปแบบการใช้ PHP ติดต่อกับ SQL Server Database ผ่าน function ของ sqlsrv แบบกระทัดรัด รวดรัด ว่าในแต่ล่ะรูปแบบที่จำเป็นต่อการใช้งานนั้นมีรูปแบบอย่างไรบ้าง ซึ่งในบทความนี้แทบจะเรียกได้ว่า ครบสำหรับการใช้งานพื้นฐานทั่ว ๆ ไปเลยทีเดียว
จะเห็นได้ว่าในการ Query ผ่าน sqlsrv จะเลือกใช้แบบ Prepared Statement หรือแบบ Parameters Query ทั้งนี้เพื่อเป็นการลดปัญหาหรือข้อผิดพลาด ช่องโหว์ ที่จะให้การทำงานของโปรแกรมนั้นผิดพลาดได้ เมื่อพบกับข้อมูลในรูปแบบต่าง ๆ
insert.php (การ Insert ข้อมูล)
<?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));
}
$strCustomerID = "C005";
$strName = "Weerachai Nukitram";
$strEmail = "[email protected]";
$strCountryCode = "TH";
$strBudget = "6000000";
$strUsed = "0";
$sql = "INSERT INTO customer (CustomerID, Name, Email, CountryCode, Budget, Used) VALUES (?, ?, ?, ?, ?, ?)";
$params = array($strCustomerID, $strName, $strEmail, $strCountryCode, $strBudget, $strUsed);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Record add successfully";
}
sqlsrv_close($conn);
?>
select.php (การ Select ข้อมูล แบบ 1 รายการ)
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "sa";
$userPassword = "";
$dbName = "mydatabase";
$strCustomerID = "C001";
$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);
echo $result["Name"]
?>
list.php (การ Select ข้อมูลบบแสดงรายการ)
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "sa";
$userPassword = "";
$dbName = "mydatabase";
$strCustomerID = "C001";
$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);
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
echo $result["Name"];
}
?>
update.php (การ Update ข้อมูล)
<?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));
}
$strName = "Weerachai Nukitram";
$strEmail = "[email protected]";
$strCountryCode = "TH";
$strBudget = "6000000";
$strUsed = "100000";
$strCustomerID = "C005";
$sql = "UPDATE customer SET
Name = ? ,
Email = ? ,
CountryCode = ? ,
Budget = ? ,
Used = ?
WHERE CustomerID = ? ";
$params = array($strName, $strEmail, $strCountryCode, $strBudget, $strUsed,$strCustomerID);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Record update successfully";
}
sqlsrv_close($conn);
?>
delete.php (การ Delete ข้อมูล)
<?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));
}
$strCustomerID = "C005";
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "DELETE FROM customer
WHERE CustomerID = ? ";
$params = array($strCustomerID);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Record delete successfully";
}
sqlsrv_close($conn);
?>
storet.php (การเรียกใช้งาน Store Procedure)
<?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));
}
$sql = "{call my_stored_procedure(?, ?, ?)}";
$params = array(
array($parameter1, SQLSRV_PARAM_IN),
array($parameter2, SQLSRV_PARAM_IN),
array($output_parameter, SQLSRV_PARAM_OUT)
);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close($conn);
?>
|