Ïê½âThinkPHP5ÅÌÎÊÊý¾ÝºÍ´òÓ¡µÄÒªÁì
½üÄêÀ´£¬Ëæ×Å»¥ÁªÍøÊÖÒյĿìËÙÉú³¤£¬web ¿ª·¢ÒѳÉΪһÖÖÆÕ±éÓ¦ÓÃÓÚÖÖÖÖÁìÓòµÄÊÖÒÕ£¬¶ø php ×÷ΪЧÀÍÆ÷¶Ë±à³ÌÓïÑÔ£¬Òѱ»ÆÕ±éÔËÓÃÓÚweb¿ª·¢ÁìÓò¡£ÔÚphpÖУ¬thinkphpÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢¡¢¼òÆÓÒ×ÓõÄweb¿ª·¢¿ò¼Ü£¬ÒѾ³ÉΪÁËÐí¶à¿ª·¢ÕßµÄÊ×Ñ¡¡£±¾ÎĽ«ÖصãÏÈÈÝÔÚthinkphp5¿ò¼ÜÏÂÅÌÎÊÊý¾ÝºÍ´òÓ¡µÄÒªÁì¡£
»ù´¡ÅÌÎÊ
ÔÚThinkPHP5ÖУ¬Ê¹ÓÃÖúÊÖº¯Êýdb()À´²Ù×÷Êý¾Ý¿â£¬ÆäÖÐÅÌÎÊÊý¾ÝµÄÒªÁìÓÐÐí¶à£¬×î³£ÓõÄÊÇselect()ºÍfind()¡£
select()ÒªÁìÅÌÎʶàÌõÊý¾Ý£¬·µ»ØÒ»¸ö°üÀ¨¶à¸öÊý×éµÄ¶þάÊý×é¡£
find()ÒªÁìÅÌÎÊÒ»ÌõÊý¾Ý£¬·µ»ØÒ»¸öһάÊý×é¡£
ÀýÈ磬ÅÌÎÊuser±íÖеÄËùÓÐÊý¾Ý£º
use think\facade\Db; $users = Db::table('user')->select();
µÇ¼ºó¸´ÖÆ
ÅÌÎÊuser±íÖÐidΪ1µÄÊý¾Ý£º
$user = Db::table('user')->where('id', 1)->find();
µÇ¼ºó¸´ÖÆ
ThinkPHP5Ö§³ÖÁ´Ê½²Ù×÷£¬¿ÉÒÔÔÚÒ»¸öÒªÁìºóÖ±½Ó½Ó×ÅʹÓÃÆäËûÒªÁ죬ÒÔ±ãÔ½·¢Àû±ãµØɸѡ³öÐèÒªµÄÊý¾Ý¡£ÀýÈ磬ÅÌÎÊuser±íÖÐstatusΪ1¡¢Æ¾Ö¤create_time½µÐòÅÅÁеÄÇ°10ÌõÊý¾Ý£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$users = Db::table('user') ->where('status', 1) ->order('create_time', 'desc') ->limit(10) ->select();
µÇ¼ºó¸´ÖÆ
¸ß¼¶ÅÌÎÊ
³ýÁË»ù´¡ÅÌÎÊÖ®Í⣬ThinkPHP5»¹ÌṩÁËһЩ¸ß¼¶ÅÌÎÊÓï·¨£¬ÒÔ±ãÔ½·¢Àû±ãµØÅÌÎÊÊý¾Ý¡£
2.1 »ØÊÓ²ìѯ
»ØÊÓ²ìѯÊÇÒ»ÖÖÁ´Ê½²Ù×÷µÄ·½·¨£¬ËüʹÓÃwhere()ÒªÁì´«ÈëÒ»¸öÄäÃûº¯Êý£¬²¢½«ÅÌÎÊÌõ¼þ×÷Ϊ¸Ãº¯ÊýµÄ²ÎÊý£¬ËüµÄÖ´ÐÐЧ¹û¾ÍÊÇÅÌÎÊÌõ¼þ£¬È»ºó·µ»ØÒ»¸öÅÌÎʹ¹½¨Æ÷¹¤¾ß¡£ÀýÈ磬ÅÌÎÊuser±íÖÐname¼´ÊÇleijun»òÕßemail¼´ÊÇleijun@gmail.comµÄËùÓÐÊý¾Ý£º
$users = Db::table('user')->where(function($query){ $query->where('name', 'leijun') ->whereOr('email', 'leijun@gmail.com'); })->select();
µÇ¼ºó¸´ÖÆ
2.2 likeÅÌÎÊ
likeÅÌÎÊÊÇÒ»ÖÖÄ£ºýÅÌÎÊ·½·¨£¬ËüʹÓÃwhere()ÒªÁì´«ÈëÒ»¸öÒÔlikeΪÌõ¼þµÄ×Ö·û´®£¬²¢½«ÅÌÎÊÌõ¼þ×÷Ϊ¸Ã×Ö·û´®µÄ²ÎÊý£¬È»ºó·µ»ØÒ»¸öÅÌÎʹ¹½¨Æ÷¹¤¾ß¡£ÀýÈ磬ÅÌÎÊuser±íÖÐnameÒÔleijun¿ªÍ·µÄËùÓÐÊý¾Ý£º
$users = Db::table('user')->where('name', 'like', 'leijun%')->select();
µÇ¼ºó¸´ÖÆ
2.3 inÅÌÎÊ
inÅÌÎÊÊÇÒ»ÖÖÔÚÒ»×éÊý¾ÝÖÐÅÌÎʵķ½·¨£¬ËüʹÓÃwhereIn()ÒªÁìÀ´ÊµÏÖ£¬¸ÃÒªÁì½ÓÊÜÒ»¸ö×Ö¶ÎÃûºÍÒ»¸öÊý×é×÷Ϊ²ÎÊý£¬·µ»ØÒ»¸öÅÌÎʹ¹½¨Æ÷¹¤¾ß¡£ÀýÈ磬ÅÌÎÊuser±íÖÐidÔÚ[1,2,3]ÖеÄËùÓÐÊý¾Ý£º
$users = Db::table('user')->whereIn('id', [1,2,3])->select();
µÇ¼ºó¸´ÖÆ
Êý¾Ý´òÓ¡
ͨ¹ýÉÏÃæµÄÅÌÎÊÒªÁ죬ÎÒÃÇÒѾ»ñµÃÁËÎÒÃÇÏëÒªµÄÊý¾Ý£¬½ÓÏÂÀ´ÐèÒª½«Êý¾Ý´òÓ¡³öÀ´¡£
ThinkPHP5µÄÊý¾Ý´òÓ¡ÒªÁìºÜÊǼòÆÓ£¬ÎÒÃÇÖ»ÐèҪʹÓÃdump()»òvar_dump()º¯Êý¼´¿É¡£ÀýÈ磬´òÓ¡ÅÌÎʵ½µÄuser±íÖеÄËùÓÐÊý¾Ý£º
use think\facade\Db; $users = Db::table('user')->select(); dump($users);
µÇ¼ºó¸´ÖÆ
ÔÚä¯ÀÀÆ÷Öз¿ª¸ÃÒ³Ã棬¾Í»á´òÓ¡³öÅÌÎʵ½µÄÊý¾Ý¡£
½áÓï
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÄúÒѾÏàʶÁËÔÚThinkPHP5¿ò¼ÜÏÂÅÌÎÊÊý¾ÝºÍ´òÓ¡µÄÒªÁ죬ÆäÖÐÎÒÃÇÖ÷ÒªÏÈÈÝÁË»ù´¡ÅÌÎÊ¡¢¸ß¼¶ÅÌÎʺÍÊý¾Ý´òÓ¡¡£ÕÆÎÕÕâЩ֪ʶ£¬ÐÅÍÐÄúÒѾ¿ÉÒÔÔÚ¿ª·¢ÖоÙÐиßЧµÄÊý¾ÝÅÌÎʺʹòÓ¡ÁË¡£
ÒÔÉϾÍÊÇÏê½âThinkPHP5ÅÌÎÊÊý¾ÝºÍ´òÓ¡µÄÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡