thinkphp3.2ÔõÑùÔöÌíÊý¾Ý¿âÉèÖò¢²Ù×÷Êý¾Ý
Ò»¡¢Êý¾Ý¿âÉèÖÃ
ÔÚconfigÎļþ¼ÐÖÐн¨database.phpÎļþ£¬Ìí¼ÓÒÔÏÂÉèÖãº
return array( 'DB_TYPE' => 'mysql', // Êý¾Ý¿âÀàÐÍ 'DB_HOST' => 'localhost', // ЧÀÍÆ÷µØµã 'DB_NAME' => 'dbname', // Êý¾Ý¿âÃû 'DB_USER' => 'username', // Óû§Ãû 'DB_PWD' => 'password', // ÃÜÂë 'DB_PORT' => '3306', // ¶Ë¿Ú 'DB_PREFIX' => '', // Êý¾Ý¿â±íǰ׺ );
µÇ¼ºó¸´ÖÆ
ÐÞ¸ÄÊý¾Ý¿âÉèÖá£ÔÚÉèÖÃÎļþconfig.phpÖмÓÈëÒÔÏ´úÂ룺
// Êý¾Ý¿âÉèÖÃÐÅÏ¢ 'DB_CONFIG1' => include 'database.php',
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏÉèÖã¬ÎÒÃǽ«Êý¾Ý¿âÉèÖÃÎļþdatabase.phpÌí¼Óµ½ÁËconfig.phpÖУ¬²¢ÉèÖÃÁËÊý¾Ý¿âµÄÅþÁ¬²ÎÊý¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢Êý¾Ý¿âÅþÁ¬
ÔÚmodelÎļþ¼ÐÖÐн¨PublicModel.class.phpÎļþ£¬Ìí¼ÓÒÔÏ´úÂ룺
<?php namespace Model; use Think\Model; class PublicModel extends Model { protected $db_config = 'DB_CONFIG1'; }
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ´úÂ룬ÎÒÃǽ¨ÉèÁËÒ»¸öPublicModelÀà¼ÌÐø×ÔThinkPHPÌṩµÄModelÀ࣬¶ø$db_config±äÁ¿Ö¸Ã÷ÎúÅþÁ¬µÄÉèÖá£
Êý¾Ý¿âÅþÁ¬Ê¹Óá£ÔÚ¿ØÖÆÆ÷ÖпÉÒÔÖ±½ÓʹÓÃPublicModelÀ࣬ʹÓÃÒªÁìÈçÏ£º
$PublicModel = M('Public');
µÇ¼ºó¸´ÖÆ
ÎÒÃÇʹÓÃMº¯ÊýŲÓÃPublicModelÄ£×Ó£¬´Ó¶ø½¨ÉèÁËÒ»¸öPublicModel¹¤¾ß¡£ÕâÑùÎÒÃǾͽ¨ÉèÁËÅþÁ¬µ½Êý¾Ý¿âµÄÅþÁ¬¡£
Èý¡¢Êý¾Ý²Ù×÷
ÔÚÅþÁ¬ÁËÊý¾Ý¿âÖ®ºó£¬ÎÒÃÇ»¹ÐèÒª¾ÙÐÐÊý¾Ý²Ù×÷£¬°üÀ¨ÔöÌí¡¢Ð޸ĺÍɾ³ýµÈ²Ù×÷¡£ÏÂÃæ»®·ÖÏÈÈÝÕâЩ²Ù×÷¡£
Êý¾ÝÅÌÎÊ
$PublicModel = M('Public'); $list = $PublicModel->where('id=1')->find();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¶ÔPublicModelÄ£×Ó¾ÙÐÐÁËÒ»´ÎÅÌÎÊ£¬²¢½«idΪ1µÄ¼Í¼·µ»Ø¡£
Êý¾Ý²åÈë
$PublicModel = M('Public'); $data['username'] = 'tom'; $data['password'] = '123456'; $id = $PublicModel->add($data);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¶ÔPublicModelÄ£×Ó¾ÙÐÐÁËÒ»´Î²åÈë²Ù×÷£¬½«Óû§ÃûΪtom£¬ÃÜÂëΪ123456µÄ¼Í¼²åÈëµ½Êý¾Ý¿â£¬²¢·µ»Ø²åÈëµÄ¼Í¼IDºÅ¡£
Êý¾ÝÐÞ¸Ä
$PublicModel = M('Public'); $data['username'] = 'tom'; $map['id'] = 1; $result = $PublicModel->where($map)->save($data);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¶ÔPublicModelÄ£×Ó¾ÙÐÐÁËÒ»´ÎÐ޸IJÙ×÷£¬½«idΪ1µÄ¼Í¼µÄÓû§Ãû¸ÄΪtom£¬²¢·µ»Ø²Ù×÷Ч¹û¡£
Êý¾Ýɾ³ý
$PublicModel = M('Public'); $map['id'] = 1; $result = $PublicModel->where($map)->delete();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¶ÔPublicModelÄ£×Ó¾ÙÐÐÁËÒ»´Îɾ³ý²Ù×÷£¬É¾³ýidΪ1µÄ¼Í¼£¬²¢·µ»Ø²Ù×÷Ч¹û¡£
ÒÔÉϾÍÊÇthinkphp3.2ÔõÑùÔöÌíÊý¾Ý¿âÉèÖò¢²Ù×÷Êý¾ÝµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡