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 กับการนำ array_push มาเก็บค่า result ของ mysql ที่ได้จากการ query และ fetch array



 
Clound SSD Virtual Server

php กับการนำ array_push มาเก็บค่า result ของ mysql ที่ได้จากการ query และ fetch array

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

ตาราง customer
CREATE TABLE `customer` (
  `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;

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

INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John  Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);


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

php mysql result array_push

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

รูปแบบ

- ใช้ fetch แบบธรรมดา
	$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
	$objDB = mysql_select_db("mydatabase");
	$strSQL = "SELECT * FROM customer";
	$objQuery = mysql_query($strSQL) or die (mysql_error());
	while($objResult = mysql_fetch_array($objQuery))
	{
	..... echo $objResult["CustomerID"];....
	}
	mysql_close($objConnect);

- ใช้การ fetch และ push ลงใน array
	$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
	$objDB = mysql_select_db("mydatabase");
	$strSQL = "SELECT * FROM customer";
	$objQuery = mysql_query($strSQL) or die (mysql_error());
	$resultArray = array();
	while($obResult = mysql_fetch_array($objQuery))
	{
		array_push($resultArray,$obResult);
	}
	mysql_close($objConnect);
	// $resultArray;


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

array(4) {
  [0]=>
  array(12) {
    [0]=>
    string(4) "C001"
    ["CustomerID"]=>
    string(4) "C001"
    [1]=>
    string(13) "Win Weerachai"
    ["Name"]=>
    string(13) "Win Weerachai"
    [2]=>
    string(28) "[email protected]"
    ["Email"]=>
    string(28) "[email protected]"
    [3]=>
    string(2) "TH"
    ["CountryCode"]=>
    string(2) "TH"
    [4]=>
    string(7) "1000000"
    ["Budget"]=>
    string(7) "1000000"
    [5]=>
    string(6) "600000"
    ["Used"]=>
    string(6) "600000"
  }
  [1]=>
  array(12) {
    [0]=>
    string(4) "C002"
    ["CustomerID"]=>
    string(4) "C002"
    [1]=>
    string(11) "John  Smith"
    ["Name"]=>
    string(11) "John  Smith"
    [2]=>
    string(25) "[email protected]"
    ["Email"]=>
    string(25) "[email protected]"
    [3]=>
    string(2) "EN"
    ["CountryCode"]=>
    string(2) "EN"
    [4]=>
    string(7) "2000000"
    ["Budget"]=>
    string(7) "2000000"
    [5]=>
    string(6) "800000"
    ["Used"]=>
    string(6) "800000"
  }
  [2]=>
  array(12) {
    [0]=>
    string(4) "C003"
    ["CustomerID"]=>
    string(4) "C003"
    [1]=>
    string(9) "Jame Born"
    ["Name"]=>
    string(9) "Jame Born"
    [2]=>
    string(24) "[email protected]"
    ["Email"]=>
    string(24) "[email protected]"
    [3]=>
    string(2) "US"
    ["CountryCode"]=>
    string(2) "US"
    [4]=>
    string(7) "3000000"
    ["Budget"]=>
    string(7) "3000000"
    [5]=>
    string(6) "600000"
    ["Used"]=>
    string(6) "600000"
  }
  [3]=>
  array(12) {
    [0]=>
    string(4) "C004"
    ["CustomerID"]=>
    string(4) "C004"
    [1]=>
    string(12) "Chalee Angel"
    ["Name"]=>
    string(12) "Chalee Angel"
    [2]=>
    string(27) "[email protected]"
    ["Email"]=>
    string(27) "[email protected]"
    [3]=>
    string(2) "US"
    ["CountryCode"]=>
    string(2) "US"
    [4]=>
    string(7) "4000000"
    ["Budget"]=>
    string(7) "4000000"
    [5]=>
    string(6) "100000"
    ["Used"]=>
    string(6) "100000"
  }
}


ซึ่งจะเห็นว่าโครงสร้างเก็บเหมือนกับการ fetch ด้วย array ทุกประการ








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

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

function returnCustomer()
{
	$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
	$objDB = mysql_select_db("mydatabase");
	$strSQL = "SELECT * FROM customer";
	$objQuery = mysql_query($strSQL) or die (mysql_error());
	$resultArray = array();
	while($obResult = mysql_fetch_array($objQuery))
	{
		array_push($resultArray,$obResult);
	}
	mysql_close($objConnect);
	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 mysql result array_push

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








บทความที่เกี่ยวข้อง
Go to : PHP MySQL List Record
Go to : PHP MySQL : Connect to MySQL Database ภาษา PHP กับฐานข้อมูล MySQL


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2012-06-07
  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 02
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 อัตราราคา คลิกที่นี่