thinkphpÅÌÎÊÊýÄ¿
thinkphpÊÇÒ»¸öÓÅÒìµÄphp¿ª·¢¿ò¼Ü£¬¿ÉÒÔ×ÊÖú¿ª·¢Õß¿ìËÙ¿ª·¢¾ßÓи߿ÉÀ©Õ¹ÐÔ¡¢¸ßЧÂÊ¡¢¸ßÇå¾²ÐÔµÄwebÓ¦ÓóÌÐò¡£ÔÚʹÓÃthinkphp¿ò¼Üʱ£¬ÅÌÎÊÊý¾ÝÊÇ¿ª·¢Àú³ÌÖÐ×îΪ³£¼ûµÄ²Ù×÷Ö®Ò»¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÖصãÏÈÈÝÔõÑùʹÓÃthinkphp¿ò¼ÜÅÌÎÊÊý¾ÝÊýÄ¿¡£
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃquery()ÒªÁì¡¢ModelÀà¡¢DbÀà¡¢Db¹¤¾ßµÈ·½·¨À´¾ÙÐжÔÊý¾Ý¿âµÄ²Ù×÷¡£ÏÂÃ棬ÎÒÃǽ«ÖðÒ»½â˵ÖÖÖÖÒªÁìÔõÑùÅÌÎÊÊý¾ÝÊýÄ¿¡£
ʹÓÃquery()ÒªÁìÅÌÎÊÊý¾ÝÊýÄ¿
ÅÌÎÊÊý¾ÝÊýÄ¿×î³£Óõķ½·¨¾ÍÊÇʹÓÃSQLÓï¾ä£¬¶øThinkPHP·â×°µÄquery()ÒªÁì¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËÙÖ´ÐÐSQLÅÌÎʲÙ×÷¡£ÒÔÅÌÎÊUser±íÖÐage´óÓÚ¼´ÊÇ18ËêµÄÊýĿΪÀý£¬´úÂëÈçÏ£º
$count = Db::query("SELECT COUNT(*) as count FROM user WHERE age >= 18");
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍÒ»ÏÂÉÏÊö´úÂëµÄÖ´ÐÐÀú³Ì£º
¢Ù Ê×ÏÈŲÓÃDbÀàµÄquery()ÒªÁ죬À¨ºÅÖд«ÈëÐèÒªÖ´ÐеÄSQLÓï¾ä¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¢Ú ¡°SELECT COUNT(*) as count¡±ÌåÏÖÅÌÎÊËùÓÐÇкÏÌõ¼þµÄÊý¾ÝÐÐÊý¡£
¢Û ¡°FROM user¡±ÌåÏÖ´ÓUser±íÖÐÅÌÎÊ¡£
¢Ü ¡°WHERE age >= 18¡±ÊÇÅÌÎÊÌõ¼þ£¬ÌåÏÖÅÌÎÊÄêËê´óÓÚ¼´ÊÇ18ËêµÄÓû§¡£
¢Ý ʹÓÃasÒªº¦×Ö£¬½«ÅÌÎÊЧ¹ûÃüÃûΪcount¡£
ʹÓÃquery()ÒªÁìÅÌÎÊÊý¾ÝÊýÄ¿µÄÓŵ㣺
ÎÞаÐԸߣ¬Öª×ãÖÖÖÖÐèÒªÅÌÎÊÊý¾ÝÊýÄ¿µÄÇéÐΡ£
¿ÉÒÔƾ֤SQLÓï¾äµÄÐèÒª¾ÙÐÐÅÅÐò¡¢É¸Ñ¡µÈ¡£
ʹÓÃquery()ÒªÁìÅÌÎÊÊý¾ÝÊýÄ¿µÄÈõµã£º
ÐèÒªÊÖ¶¯±àдSQLÓï¾ä£¬ÈôÊÇSQLÓï¾äÍÉ»¯»òÕßÓï¾ä²»¹æ·¶£¬»áµ¼ÖÂÅÌÎÊʧ°Ü¡£
ÄÑÒÔά»¤£¬Ò»µ©Êý¾Ý±í±¬·¢×ª±ä£¬¾ÍÐèÒªÐ޸ĴúÂëÖеÄSQLÓï¾ä£¬ºÜÈÝÒ×·ºÆðÈËΪ¹ýʧ¡£
ʹÓÃModelÀàÅÌÎÊÊý¾ÝÊýÄ¿
ÔÚThinkPHPÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýModelÀàÀ´Íê³É¶ÔÊý¾Ý±íµÄ²Ù×÷¡£ModelÀàÊÇ¿ò¼ÜÖÐ×î½¹µãµÄÀàÖ®Ò»£¬Ê¹ÓøÃÀà¿ÉÒÔÇáËɵضÔÊý¾Ý±í¾ÙÐÐÌí¼Ó¡¢Ð޸ġ¢ÅÌÎÊ¡¢É¾³ýµÈ²Ù×÷¡£
ÒÔÅÌÎÊUser±íÖÐageСÓÚ18ËêµÄÊýĿΪÀý£¬´úÂëÈçÏ£º
$count = Model::name('User')->where('age < 18')->count();
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍÒ»ÏÂÉÏÊö´úÂëµÄÖ´ÐÐÀú³Ì£º
¢Ù ʹÓÃname()ÒªÁìÖ¸¶¨²Ù×÷µÄ±íÃû£¬ÕâÀïÊÇUser¡£
¢Ú ʹÓÃwhere()ÒªÁìÌí¼ÓÅÌÎÊÌõ¼þ£¬ÅÌÎÊÄêËêСÓÚ18ËêµÄÓû§¡£
¢Û ʹÓÃcount()ÒªÁìͳ¼ÆÇкÏÌõ¼þµÄÊýÄ¿¡£
ʹÓÃModelÀàÅÌÎÊÊý¾ÝÊýÄ¿µÄÓŵ㣺
ʹÓÃÀû±ã£¬¾ßÓÐÓÅÒìµÄ¿É¶ÁÐÔ£¬ÈÝÒ×ά»¤¡£
×ñÕÕ¿ò¼ÜµÄMVCģʽ£¬ÈôúÂë¾ßÓиü¸ßµÄ¿ÉÀ©Õ¹ÐÔ¡£
ʹÓÃModelÀàÅÌÎÊÊý¾ÝÊýÄ¿µÄÈõµã£º
ModelÀàÖ»ÄܲÙ×÷Êý¾Ý±í£¬ÅÌÎÊÊý¾ÝÊýĿʱÐèÒªÌí¼Ówhere()Ìõ¼þ£¬ÏÞÖÆÁËÅÌÎÊ·½·¨µÄÎÞаÐÔ¡£
¹ØÓÚÖØ´óÅÌÎÊ£¬ÐèÒª±àдSQLÓï¾äºÍÌõ¼þÓï¾ä£¬ÔĶÁÆðÀ´Ïà¶ÔÄÑÌâ¡£
ʹÓÃDbÀàÅÌÎÊÊý¾ÝÊýÄ¿
ThinkPHP¿ò¼ÜÖеÄDbÀ࣬·â×°Á˳£ÓõÄÊý¾Ý¿â²Ù×÷¹¦Ð§£¬ÈçÔöɾ¸Ä²éµÈ¡£Ê¹ÓÃDbÀàÅÌÎÊÊý¾ÝÊýÄ¿µÄÒªÁìºÍʹÓÃquery()ÒªÁìÏàËÆ£¬ÒªÁìÈçÏ£º
$count = Db::table('user')->where('age >= 18')->count();
µÇ¼ºó¸´ÖÆ
Ú¹ÊÍÒ»ÏÂÉÏÊö´úÂëµÄÖ´ÐÐÀú³Ì£º
¢Ù ʹÓÃtable()ÒªÁìÖ¸¶¨²Ù×÷µÄ±íÃû£¬ÕâÀïÊÇUser¡£
¢Ú ʹÓÃwhere()ÒªÁìÌí¼ÓÅÌÎÊÌõ¼þ£¬ÅÌÎÊÄêËê´óÓÚ¼´ÊÇ18ËêµÄÓû§¡£
¢Û ʹÓÃcount()ÒªÁìͳ¼ÆÇкÏÌõ¼þµÄÊýÄ¿¡£
ʹÓÃDbÀàÅÌÎÊÊý¾ÝÊýÄ¿µÄÓŵ㣺
Àû±ã¿ì½Ý£¬´úÂë½á¹¹¼òÆÓ£¬Ò×ÓÚʹÓúÍά»¤¡£
¿ÉÒÔÎÞаʹÓÃÁ´Ê½²Ù×÷£¬¶ÔÅÌÎÊÓï¾ä¾ÙÐÐÌõ¼þɸѡ¡¢Á¬±íµÈ¡£
ʹÓÃDbÀàÅÌÎÊÊý¾ÝÊýÄ¿µÄÈõµã£º
¹ØÓÚÖØ´óµÄSQLÓï¾ä£¬»á½ÏÁ¿ÄÑÒÔ´¦Öóͷ£¡£
ÓÉÓÚʹÓÃÁ´Ê½²Ù×÷£¬¿ÉÄܻᱣ´æŲÓÃÒªÁìµÄ˳ÐòÎÊÌ⣬ÐèҪעÖØ¡£
×ܽá
ͨ¹ýÉÏÊöÈýÖÖÒªÁì¿ÉÒÔʵÏÖÅÌÎÊÊý¾ÝÊýÄ¿µÄ²Ù×÷¡£²î±ðµÄÅÌÎÊÒªÁì¸÷ÓÐÓŵãºÍÈõµã£¬ÐèҪƾ֤ÏÖÕæÏàÐÎÑ¡ÔñÊʺÏ×Ô¼ºµÄ·½·¨¡£×ÜÖ®£¬ThinkPHP¿ò¼ÜµÄÇ¿Ê¢ºÍÎÞаÐÔΪÎÒÃÇ¿ª·¢¸ßЧ¿É¿¿µÄWebÓ¦ÓÃÌṩÁ˶àÖÖÒªÁ죬Äܹ»Ô½·¢ÇáËɵØʵÏÖÖÖÖÖÊý¾Ý¿â²Ù×÷¡£
ÒÔÉϾÍÊÇthinkphpÅÌÎÊÊýÄ¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡