thinkphpÅÌÎÊÊý¾Ý¿â·µ»ØÊý×é
ÔÚweb¿ª·¢Àú³ÌÖУ¬Êý¾Ý¿âÅÌÎÊÊDz»¿É×èÖ¹µÄÒ»»·¡£ÆäÖУ¬thinkphp×÷Ϊһ¿îphp¿ò¼Ü£¬ÌṩÁ˸»ºñµÄÊý¾Ý¿â²Ù×÷ÒªÁ죬±¾Îļ´Ì½ÌÖÔõÑùʹÓÃthinkphpÅÌÎÊÊý¾Ý¿â²¢·µ»ØÊý×é¡£
Ò»¡¢ÇéÐÎÉèÖÃ
ÔÚʹÓÃthinkphp¾ÙÐÐÊý¾Ý¿â²Ù×÷֮ǰ£¬ÐèÒªÏȾÙÐÐһЩÇéÐÎÉèÖá£Ïêϸ°ì·¨ÈçÏ£º
ÔÚthinkphpÏîÄ¿µÄ¸ùĿ¼ÏÂÕÒµ½database.phpÎļþ£¬¸ÃÎļþÊÇthinkphpÊý¾Ý¿âÉèÖÃÎļþ£¬·¿ª¸ÃÎļþ£¬Æ¾Ö¤Êý¾Ý¿âµÄÏà¹ØÐÅÏ¢ÐÞ¸ÄÒÔÏÂ×ֶΣº
// Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'database_name', // Êý¾Ý¿âÓû§Ãû 'username' => 'root', // Êý¾Ý¿âÃÜÂë 'password' => 'root', // Êý¾Ý¿â±àÂë 'charset' => 'utf8mb4', // Êý¾Ý¿â±íǰ׺ 'prefix' => '',
µÇ¼ºó¸´ÖÆ
ÔÚÉèÖÃÎļþÖУ¬ÎÒÃÇ»¹ÐèÒªÉèÖÃÊý¾Ý¿âµÄÅþÁ¬ÐÅÏ¢¡£¿ÉÒÔÔÚconfig.phpÖÐÌí¼ÓÒÔÏ´úÂ룺
// Êý¾Ý¿âÅþÁ¬²ÎÊýÉèÖà 'db_config' => [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'database_name', // Êý¾Ý¿âÓû§Ãû 'username' => 'root', // Êý¾Ý¿âÃÜÂë 'password' => 'root', // Êý¾Ý¿â±àÂë 'charset' => 'utf8mb4', // Êý¾Ý¿â±íǰ׺ 'prefix' => '', // Êý¾Ý¿âÅþÁ¬²ÎÊý 'params' => [ PDO::ATTR_CASE => PDO::CASE_NATURAL, // ²»¾ÙÐоÞϸдת»» PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Å׳öÒì³£ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // ĬÈÏÒÔ¹ØÁªÊý×éÐÎʽ·µ»ØÊý¾Ý ], ],
µÇ¼ºó¸´ÖÆ
ÔÚconfig.phpÖÐÌí¼ÓÈçÏ´úÂëÆôÓÃÊý¾Ý¿âÉèÖü°²ÎÊý£º
// Êý¾Ý¿âÏà¹ØÉèÖà 'default_return_type' => 'array', // ĬÈÏ·µ»ØÊý¾Ý¼¯ÀàÐÍΪÊý×é // Êý¾Ý¿âÉèÖà 'db_config' => require_once(APP_PATH.'database.php'), 'database' => $db_config['database'], // Êý¾Ý¿âÃû³Æ 'prefix' => $db_config['prefix'], // ±íǰ׺
µÇ¼ºó¸´ÖÆ
ÖÁ´Ë£¬ÎÒÃǵÄÇéÐÎÉèÖÃÍê³É¡£
¶þ¡¢Êý¾Ý¿âÅÌÎʲÙ×÷
ÏëÒªÅÌÎÊÊý¾Ý¿â²¢·µ»ØÊý×飬ÎÒÃÇÐèҪʹÓÃthinkphp·â×°µÄDbÀàÌṩµÄÏà¹ØÒªÁì¡£ÏÂÃæÒÔÅÌÎÊÓû§±íΪÀý¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÅÌÎÊËùÓÐÓû§
$users = Db::name('user')->select(); dump($users);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Db::name(‘user’)ÌåÏÖÅÌÎÊuser±í£¬select()ÌåÏÖÅÌÎÊuser±íÖеÄËùÓÐÊý¾Ý£¬²¢½«Ð§¹û´æµ½$users±äÁ¿ÖС£dump()º¯Êý¿ÉÒÔÊä³ö±äÁ¿µÄÏêϸÐÅÏ¢£¬±ãÓÚÎÒÃǵ÷ÊÔ´úÂë¡£
ÅÌÎʵ¥¸öÓû§
$user = Db::name('user')->where('id', 1)->find(); dump($user);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬where()º¯ÊýÌåÏÖÅÌÎÊidΪ1µÄÓû§£¬find()º¯ÊýÌåÏÖÅÌÎʲ¢·µ»ØÒ»ÌõÊý¾Ý¡£$user±äÁ¿Öд洢µÄ¼´ÎªÅÌÎÊЧ¹û¡£
ÅÌÎÊÊý¾Ý×ÜÊý
$count = Db::name('user')->count(); echo $count;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬count()º¯Êý¿ÉÒÔ·µ»Øuser±íÖÐÊý¾ÝµÄ×ÜÊý¡£ÎÒÃÇ¿ÉÒÔʹÓÃecho½«ÆäÊä³ö¡£
ÅÌÎÊÓû§ÐÕÃû
$usernames = Db::name('user')->column('name'); dump($usernames);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬column(‘name’)ÌåÏÖÖ»ÅÌÎÊuser±íÖеÄnameÁУ¬$usernames±äÁ¿Öд洢µÄ¼´ÎªÅÌÎÊЧ¹û¡£
ÅÌÎÊÓû§ÐÕÃûºÍÄêËê
$userinfos = Db::name('user')->field('name,age')->select(); dump($userinfos);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬field(‘name,age’)ÌåÏÖÖ»ÅÌÎÊuser±íÖеÄnameºÍageÁ½ÁУ¬$userinfos±äÁ¿Öд洢µÄ¼´ÎªÅÌÎÊЧ¹û¡£
ÅÌÎÊÄêËê´óÓÚ20ËêµÄÓû§
$users = Db::name('user')->where('age', '>', 20)->select(); dump($users);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬where(‘age’, ‘>’, 20)ÌåÏÖÅÌÎÊage´óÓÚ20µÄÓû§£¬$users±äÁ¿Öд洢µÄ¼´ÎªÅÌÎÊЧ¹û¡£
ʹÓÃÔÉúSQLÓï¾äÅÌÎÊ
$users = Db::query('select * from user'); dump($users);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬Db::query()¿ÉÒÔʹÓÃÔÉúSQLÓï¾äÅÌÎÊÊý¾Ý¿â¡£
Èý¡¢ÅÌÎÊЧ¹ûµÄ·µ»ØÀàÐÍ
thinkphpÖ§³Ö¶àÖÖÅÌÎÊЧ¹ûµÄ·µ»ØÀàÐÍ¡£ÏÂÃæÏÈÈÝһЩ³£¼ûµÄ·µ»ØÀàÐÍ¡£
Êý×é
ÒÔÉÏ´úÂëÖÐÎÒÃÇÒѾÏàʶµ½£¬thinkphpĬÈÏ·µ»ØÊý×éÀàÐ͵ÄÅÌÎÊЧ¹û¡£¿ÉÒÔÔÚconfig.phpÖÐÌí¼ÓÈçÏ´úÂ룬ָ¶¨Ä¬ÈÏ·µ»Ø·½·¨£º
'default_return_type' => 'array',
µÇ¼ºó¸´ÖÆ
¹¤¾ß
ÎÒÃÇ¿ÉÒÔÉèÖÃĬÈÏ·µ»Ø¹¤¾ßÀàÐ͵ÄÅÌÎÊЧ¹û¡£ÔÚconfig.phpÖÐÌí¼ÓÈçÏ´úÂ룺
'default_return_type' => 'object',
µÇ¼ºó¸´ÖÆ
JSON
ÎÒÃÇ¿ÉÒÔÉèÖ÷µ»ØjsonÀàÐ͵ÄÅÌÎÊЧ¹û¡£ÔÚconfig.phpÖÐÌí¼ÓÈçÏ´úÂ룺
'default_return_type' => 'json',
µÇ¼ºó¸´ÖÆ
ËÄ¡¢×ܽá
±¾ÎÄÖ÷ÒªÏÈÈÝÁËÔõÑùʹÓÃthinkphpÅÌÎÊÊý¾Ý¿â²¢·µ»ØÊý×é¡£ÆäÖУ¬ÎÒÃÇÏàʶÁËÇéÐÎÉèÖá¢Êý¾Ý¿âÅÌÎʲÙ×÷¡¢ÅÌÎÊЧ¹ûµÄ·µ»ØÀàÐ͵ÈÄÚÈÝ¡£ÔÚÏÖʵ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇÐèҪƾ֤ÏêϸÏîÄ¿ÐèÇó£¬Ñ¡ÔñºÏÊʵÄÅÌÎÊ·½·¨ºÍЧ¹û·µ»ØÀàÐÍ¡£Í¨¹ýѧϰ±¾ÎÄ£¬ÐÅÍÐÄú¶ÔthinkphpµÄÊý¾Ý¿â²Ù×÷ÓÐÁ˸üÉîÈëµÄÏàʶ¡£
ÒÔÉϾÍÊÇthinkphpÅÌÎÊÊý¾Ý¿â·µ»ØÊý×éµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡