Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > PHP กับ MySQLi การประยุคใช้กับ array_push เพื่อนำค่า Result ไปใช้ได้หลายครั้ง



 
Clound SSD Virtual Server

PHP กับ MySQLi การประยุคใช้กับ array_push เพื่อนำค่า Result ไปใช้ได้หลายครั้ง

PHP กับ MySQLi การประยุคใช้กับ array_push เพื่อนำค่า Result ไปใช้ได้หลายครั้ง เป็นเทคนิคเล็ก ๆ น้อย ของการเขียน PHP กับ MySQLi และมันไม่ใช่เรื่องใหม่ แต่คิดว่าหลาย ๆ คนคงจะยังไม่เคยใช้ เพราะวิธีนี้ช่วยแยกการทำงานระหว่าง result ของ mysqli เพราะในกรณีที่เราได้ค่า result โดยผ่านการ fetch array แล้ว เราสามารถคำค่าที่ได้ทั้งหมด มาเก็บไว้ใน array เพื่อเอาค่าที่ได้ไปใช้งานในส่วนอื่น ๆ ได้ มีประโยชน์เช่น สามารถนำไป loop ค่าได้หลายครั้ง โดยไม่ต้อง query อีกรอบ อีกทั้งยังสามารถแยกการทำงานกับ mysqli ได้อย่างชัดเจน เหมาะสำหรับการ return ค่าผ่าน function เพราะจะช่วยลดการทำงานของ php กับ mysqli แต่จะโยนภาระให้ memory เป็นคนเก็บค่า result เหล่านั้นแทน และสำหรับการแสดงค่าที่ผ่าน array_push สามารถใช้ loop for หรือ foreach ในการแสดงค่าทั้งหมดของ array ออกมา



ตาราง customer
CREATE TABLE IF NOT EXISTS `customer` (
  `CustomerID` varchar(4) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `CountryCode` varchar(2) NOT NULL,
  `Budget` decimal(18,2) NOT NULL,
  `Used` decimal(18,2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`CustomerID`, `Name`, `Email`, `CountryCode`, `Budget`, `Used`) VALUES
('C001', 'Win Weerachai', '[email protected]', 'TH', '1000000.00', '600000.00'),
('C002', 'John  Smith', '[email protected]', 'UK', '2000000.00', '800000.00'),
('C003', 'Jame Born', '[email protected]', 'US', '3000000.00', '600000.00'),
('C004', 'Chalee Angel', '[email protected]', 'US', '4000000.00', '100000.00');


นำ SQL นี้ไปรันบน phpMyAdmin เพื่อสร้าง table ซึ่งจะได้ table ชื่อว่า customer และมีข้อมูลอยู่ 4 รายการดังรูป

php mysqli result array_push

รูปโครงสร้าง table และ data

รูปแบบ

- ใช้ fetch แบบธรรมดา
	$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
	$sql = "SELECT * FROM customer";
	$query = mysqli_query($conn,$sql);
	if (!$query) {
		printf("Error: %s\n", $conn->error);
		exit();
	}
	while($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
	{
		...echo $result["CustomerID"];
	}
	mysqli_close($conn);

- ใช้การ fetch และ push ลงใน array
	$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
	$sql = "SELECT * FROM customer";
	$query = mysqli_query($conn,$sql);
	if (!$query) {
		printf("Error: %s\n", $conn->error);
		exit();
	}
	$resultArray = array();
	while($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
	{
		array_push($resultArray,$result);
	}
	mysqli_close($conn);

	echo '<pre>';
	var_dump($resultArray);
	echo '</pre><hr />';


ในแบบแรกจะเป็นการ fetch ข้อมูลในรูปแบบของ array ออกมาทั้งหมด แต่ในแบบที่ 2 ในการ fetch ข้อมูลและมีการใช้ array_push ข้อมูลที่ได้เก็บลงในตัวแปร $resultArray








โดยเราสามารถนำตัวแปรนี้ไปใช้อย่างอื่นได้อีก ซึ่งเมื่อดูโครงสร้างข้อมูลที่ผ่านการใช้ array_push จะได้เป็น

array(4) { [0]=> array(6) { ["CustomerID"]=> string(4) "C001" ["Name"]=> string(13) "Win Weerachai" ["Email"]=> string(28) "[email protected]" ["CountryCode"]=> string(2) "TH" ["Budget"]=> string(10) "1000000.00" ["Used"]=> string(9) "600000.00" } [1]=> array(6) { ["CustomerID"]=> string(4) "C002" ["Name"]=> string(11) "John Smith" ["Email"]=> string(25) "[email protected]" ["CountryCode"]=> string(2) "UK" ["Budget"]=> string(10) "2000000.00" ["Used"]=> string(9) "800000.00" } [2]=> array(6) { ["CustomerID"]=> string(4) "C003" ["Name"]=> string(9) "Jame Born" ["Email"]=> string(24) "[email protected]" ["CountryCode"]=> string(2) "US" ["Budget"]=> string(10) "3000000.00" ["Used"]=> string(9) "600000.00" } [3]=> array(6) { ["CustomerID"]=> string(4) "C004" ["Name"]=> string(12) "Chalee Angel" ["Email"]=> string(27) "[email protected]" ["CountryCode"]=> string(2) "US" ["Budget"]=> string(10) "4000000.00" ["Used"]=> string(9) "100000.00" } }


ซึ่งจะเห็นว่าโครงสร้างเก็บเหมือนกับการ fetch ด้วย array ทุกประการ ซึ่งเราสามารถนำค่าตัวแปร $resultArray ที่ได้จาก array_push ไปใช้ยังส่วนอื่น ๆ ของโปรแกรมได้หลายครั้ง โดยสามารถใช้ foreach ในการ fetch ค่าออกมาแสดง ดังตัวอย่างนี้

Sample1.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php

ini_set('display_errors', 1);
error_reporting(~0);

function returnCustomer()
{
	$serverName = "localhost";
	$userName = "root";
	$userPassword = "root";
	$dbName = "mydatabase";

	$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
	$sql = "SELECT * FROM customer";
	$query = mysqli_query($conn,$sql);
	if (!$query) {
		printf("Error: %s\n", $conn->error);
		exit();
	}
	$resultArray = array();
	while($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
	{
		array_push($resultArray,$result);
	}
	mysqli_close($conn);
	return $resultArray;
}

$resultCus = returnCustomer();

//echo '<pre>';
//var_dump($resultCus);
//echo '</pre><hr />';

?>

<!--------------------------- Statement 1 ----------------------------->
<h1>Statement 1</h1>
<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>
<?php
foreach ($resultCus as $result)
{
?>
  <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>
  </tr>
<?php
}
?>
</table>

<!--------------------------- Statement 2 ----------------------------->
<h1>Statement 2</h1>
<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>
<?php
rsort($resultCus);
foreach ($resultCus as $result)
{
?>
  <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>
  </tr>
<?php
}
?>
</table>

</body>
</html>



Screenshot

php mysqli result array_push

คำอธิบาย
จากตัวอย่างจะเห็นว่ามีการสร้าง function ชื่อ returnCustomer และมีการ return ค่า array ชื่อ $resultArray ซึ่งผ่านการ push ข้อมูลในรูปแบบ array จากนั้นก็ทำการปิดการเชื่อมต่อกับ mysqli และ return ค่ากลับไป โดยตัวแปรที่รับค่าจาก function สามารถนำตัวแปรที่ได้ไป foreach เพื่อ loop ข้อมูลแสดงออกมา โดย result ทีได้ทั้งหมด ไม่เกี่ยวข้องกับ mysqli connect อีก








บทความที่เกี่ยวข้อง
Go to : PHP MySQL Database (mysqli)


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2015-11-19
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่