Tuesday, July 10, 2007

[PHP] Foreach expord Arrary

Using PHP5's foreach "as reference" can bite you!

Three guys in my office spent about a day chasing this one's tail, that was causing aberrant behavior in the values of elements of an array. It turns out to be a consequence of the nature of references, generally.

If you create a reference to a variable, all names for that variable (including the original) BECOME REFERENCES. To paraphrase "The Highlander," if you want a name to OWN a piece of data, "there can be only one."

To illustrate this point, consider the following code:

<?php

$f = array(
0 => array('value' => 'three'),
1 => array('value' => 'three')
);

foreach ( $f as $k => &$v ) {
$v['value'] = 'one';
}

$a = $f;
$b = $f;

$b[0]['value'] = 'two';
$b[1]['value'] = 'two';

var_dump($a, $b);

?>

Upon execution, you will find that, although you would expect $a to contain two arrays with 'value' of 'one', $a and $b are identical -- i.e., the changes of []['value'] to 'two' have happened in both arrays. But, upon further examination of the var_dumps, you will see that both sets' elements [0] and [1] are preceded with "&": they are references!

The easy solution to this problem turns out to be: unset the foreach "as-reference" variable ($v) at the bottom of your foreach loop. This allows the original variable (or array member) to resume ownership of the value and dissolves its "reference-ness".

Monday, July 9, 2007

MySQL : INSERT ON DUPLICATE KEY UPDATE

INSERT ON DUPLICATE KEY UPDATE
การ insert ข้อมูลบางครั้งอาจจะเกิดขึ้นผิดพลาดได้เนื่องมาจากการที่มี Key ซ้ำกัน
ปัญหาพวกนี้ส่วนใหญ่จะมาจากการ Insert แบบ Select คือ การ select ข้อมูลจาก Table หนึ่ง ไป อีก Table หนึ่ง
ซึ่งถ้ามี การทำ PRIMARY KEY ใว้ถ้าเกิดทั้ง 2 Table นั้นซ้ำกันก็จะเกิดข้อผิดพลาดไม่สามารถ INSERT ได้
วิธีแก้ก็สามารถใช้การ UPDATE แทนได้แต่ก็ทำได้แค่การ update ไม่สามารถ INSERT ได้
วันนี้จึงนำเสนอวิธีการ INSERT พร้อมทั้ง UPDATE Record ที่ซ้ำกันไปด้วยด้วยคำสั่งนี้ครับ

ตัวอย่าง :
Table1 : id , name , age
Table2 : id , name , age

SQL> INSERT INTO table1
SELECT * FROM table1
ON DUPLICATE KEY UPDATE table2.age = table2.age+table1.age;

จาก Script นี้เมื่อมีการ INSERT ข้อมูลลงใน Table2 โดยการเรียกข้อมูลจาก Table1 มา Insert
แต่ถ้าหากมี id ซ้ำกัน ในบรรทัด ON DUPLICATE KEY UPDATE จะทำการ update แทนที่จะ insert เข้าไปใหม่แทน
ผลที่ได้ก้คือจะนำเอา age ของ Table2 มาบวกกับ age ของ Table1

Blog Guide : แนะนำตัวกันหน่อย

Blog นี้จะนำเสนอวิธีการใช้งาน Dreamweaver หรือเรียกสั่นๆว่า DW ก็แล้วกันครับ

เนื้อหาส่วนใหญ่นั้นจะไปทางด้านการใช้ DW พัฒนา webapplication ด้วยภาษา PHP สาเหตุที่ตัวผู้เขียนเองเลือกภาษานี้เพราะ ทำงานกับภาษานี้มา 5 ปีกว่าแล้ว คิดว่าน่าจะนำ PHP มาเสนอมากกว่าภาษาอื่นที่ตัวเองยังไม่ถนัด

การนำเสนอจะประกอบไปด้วยบทความ เนื้อหาที่สอนเทคนิค วิธีการใช้งานในส่วนต่างๆของ Dreamweaver ไม่ว่าจะเป็นการ
  • Connect Databse MySQL กับ DW
  • การ Query ข้อมูลผ่าน DW
  • การ INSERT , UPDATE , DELETE ข้อมูลใน MySQL ด้วย Dreamweaver
จนไปถึงการสอนการพัฒนา Web Application ด้วยตัวเอง
เนื้อหาในนี้เหมาะสำหรับผู้เริ่มต้นหัดใช้ Dreamweaver และผู้ที่สนใจจะศึกษาเพิ่มเติม

แล้วพบกับบทความใหม่ๆเร็วๆนี้ครับ

Google AdSense