Sunday, September 1, 2013

ยกตัวอย่างการดึงข้อมูล การค้นหาข้อมูลจาก DB http://yiitutorials.net/easy/yii-getting-info-from-the-database

http://yiitutorials.net/easy/yii-getting-info-from-the-database


ยกตัวอย่างการดึงข้อมูล การค้นหาข้อมูลจาก DB

$ourfish = Fish::model()->find("name = 'seahorse'");
วิธีเรียกใช้ echo $ourfish->length;
 find เฉยๆ แบบนี้ จะคืนแค่ค่าเดียวเท่านั้น 
 
$ourfishes = Fish::model()->findAll("length > 5");
หรือ 
$ourfishes = Fish::model()->findAll(array("condition"=>"length > 5","order"=>"weight","limit"=>"5"); 

วิธีเรียกใช้ 
foreach ($ourfishes as $fish):
    echo $fish->name;
endforeach;
  
 findAll สังเกตว่าแบบนี้จะ return หลายค่า มากกว่า 1 โดยวิธีใช้จะเป็นการวนลูป ด้วย foreach เพื่อรันค่าออกมาทั้งหมด
 

No comments:

Post a Comment

Blog Archive

http://cksc.co.th/site/

Total Pageviews

Popular Posts