
PHP MySQL List Data Record & Get Result Row (mysqli) บทความนี้จะเป็นตัวอย่างของ mysqli การเขียน PHP เพื่ออ่านข้อมูลจาก Database ของ MySQL มาแสดงผลทางหน้าจอด้วย function ต่าง ๆ ของ mysqli ทำงานร่วมกับการ fetch ข้อมูลในรูปแบบของ array
MySQL Table
______________________________________________________________________________________
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `customer` VALUES (‘C001’, ‘Win Weerachai’, ‘win.weerachai@thaicreate.com’, ‘TH’, 1000000, 600000);
INSERT INTO `customer` VALUES (‘C002’, ‘John Smith’, ‘john.smith@thaicreate.com’, ‘UK’, 2000000, 800000);
INSERT INTO `customer` VALUES (‘C003’, ‘Jame Born’, ‘jame.born@thaicreate.com’, ‘US’, 3000000, 600000);
INSERT INTO `customer` VALUES (‘C004’, ‘Chalee Angel’, ‘chalee.angel@thaicreate.com’, ‘US’, 4000000, 100000);
____________________________________________________________________________________
ฐานข้อมูลและตารางของ MySQL Database
Syntax รูปแบบการใช้งาน
____________________________________________________________________________
<?PHP ini_set('display_errors', 1); error_reporting(~0);
$serverName = "localhost"; $userName = "root"; $userPassword = "root"; $dbName = "mydatabase"; $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$sql = "SELECT * FROM customer"; $query = mysqli_query($conn,$sql); while($result=mysqli_fetch_array($query,MYSQLI_ASSOC)) { echo $result["CustomerID"];l }
?>
_____________________________________________________________________________
Example ตัวอย่างการเขียน PHP กับ MySQL เพื่อแสดงรายการข้อมูล ด้วย function ของ MySQLi
Screenshot
แสดงข้อมูลจาก MySQL ด้วย function ของ mysqli
Example ตัวอย่างการเขียน PHP กับ MySQL เพื่อ Insert หรือเพิ่มข้อมูล ด้วย mysqli
_______________________________________________________________________________
<?php ini_set('display_errors', 1); error_reporting(~0); $serverName = "localhost"; $userName = "root"; $userPassword = "root"; $dbName = "mydatabase"; $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName); $sql = "INSERT INTO customer (CustomerID, Name, Email, CountryCode, Budget, Used) VALUES ('".$_POST["txtCustomerID"]."','".$_POST["txtName"]."','".$_POST["txtEmail"]."' ,'".$_POST["txtCountryCode"]."','".$_POST["txtBudget"]."','".$_POST["txtUsed"]."')"; $query = mysqli_query($conn,$sql); if($query) { echo "Record add successfully"; } mysqli_close($conn); ?>
_______________________________________________________________________________
Example ตัวอย่างการเขียน PHP กับ MySQL เพื่อ Edit/Update หรือแก้ไขข้อมูล ด้วย mysqli
_______________________________________________________________________________
<?php ini_set('display_errors', 1); error_reporting(~0); $serverName = "localhost"; $userName = "root"; $userPassword = "root"; $dbName = "mydatabase"; $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName); $sql = "UPDATE customer SET Name = '".$_POST["txtName"]."' , Email = '".$_POST["txtEmail"]."' , CountryCode = '".$_POST["txtCountryCode"]."' , Budget = '".$_POST["txtBudget"]."' , Used = '".$_POST["txtUsed"]."' WHERE CustomerID = '".$_POST["txtCustomerID"]."' "; $query = mysqli_query($conn,$sql); if($query) { echo "Record update successfully"; } mysqli_close($conn); ?>
_______________________________________________________________________________
Example ตัวอย่างการเขียน PHP กับ MySQL เพื่อ Delete หรือลบข้อมูล ด้วย function ของ mysqli
_______________________________________________________________________________
<?php ini_set('display_errors', 1); error_reporting(~0); $serverName = "localhost"; $userName = "root"; $userPassword = "root"; $dbName = "mydatabase"; $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName); $strCustomerID = $_GET["CustomerID"]; $sql = "DELETE FROM customer WHERE CustomerID = '".$strCustomerID."' "; $query = mysqli_query($conn,$sql); if(mysqli_affected_rows($conn)) { echo "Record delete successfully"; } mysqli_close($conn); ?>
______________________________________________________________________
บทความโดย www.thaicreate.com
Link http://www.thaicreate.com/php/php-mysql-mysqli.html