1111111111111


เว็บใหม่ที่ผมขอใช้ฟรีครับ ภาวนาอยู่ว่าวันดีคืนดีข้อมูลคงไม่หายเหมือนเว็บอื่น

วันพุธที่ 6 เมษายน พ.ศ. 2554

ตัวอย่าง Yii เชื่อมต่อกับฐานข้อมูลโดยไม่ใช้ Active Record ครับ

เป็นตัวอย่าง Yii เชื่อมต่อกับฐานข้อมูลโดยไม่ใช้ Active Record ครับ

ตัวอย่างอยู่ที่ http://php.bungkhan.com/province/province/sql ครับ

ขั้นตอนมีดังนี้

ขั้นตอนที่ 1. กำหนดการเชื่อมต่อฐานข้อมูล (http://www.yii.in.th/forum/index.php?topic=30.0)


/* ================== ถ้าอยากติดต่อ ฐานข้อมูลครับ =================*/

'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=thai',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
/*========================ถ้าอยากติดต่อ ฐานข้อมูลครับ ==============*/


ขั้นตอนที่ 2. ทำการเชื่อมต่อฐานข้อมูลและประมวลผลข้อมูล


class ProvinceController extends Controller{

public function actionSql(){


$sqlStatement ="SELECT thai_province.thaiName, thai_slogan.slogan FROM thai_slogan RIGHT Join thai_province
ON thai_slogan.provinceID = thai_province.provinceID";

$connection=Yii::app()->db;
$connection->active=true;
$command=$connection->createCommand($sqlStatement);
$command->execute();
$reader=$command->query();

$this->render('exam1', array(
'reader'=>$reader)
);
}

....
....
}



ขั้นตอนที่ 3. นำข้อมูลมาแสดงผลในส่วนของ View


foreach($reader as $row){
print "
  • ".$row['thaiName']."
  • ";
    }





  • ตัวอย่าง : http://php.bungkhan.com/province/province/sql

    ศึกษาเพิ่มเติม : http://www.yiiframework.com/doc/guide/1.1/en/database.dao

    ไกล้ปีใหม่ไทยแล้วก็ขอให้ทุกท่านมีความสูขมากๆนะครับ



    0 ความคิดเห็น: