thinkphpÅÌÎÊÖ¸¶¨×Ö¶ÎÖµµÄÒªÁìÊÇʲô
µÚÒ»²½£ºÅþÁ¬Êý¾Ý¿â
ÔÚʹÓÃThinkPHP¾ÙÐÐÊý¾Ý¿âÅÌÎÊ֮ǰ£¬±ØÐèÏÈÔÚÉèÖÃÎļþÖÐÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£ÔÚconfĿ¼ÏÂÕÒµ½database.phpÉèÖÃÎļþ£¬²¢ÉèÖúÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢
return [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'test', // Óû§Ãû 'username' => 'root', // ÃÜÂë 'password' => '', // ¶Ë¿Ú 'hostport' => '3306', // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', ];
µÇ¼ºó¸´ÖÆ
µÚ¶þ²½£ºÅÌÎÊÖ¸¶¨×ֶεÄÖµ
ʹÓÃThinkPHPÅÌÎÊÖ¸¶¨×ֶεÄÖµºÜÊǼòÆÓ¡£ÎÒÃÇ¿ÉÒÔʹÓÃselectÒªÁìÅÌÎÊÖ¸¶¨×ֶεÄÖµ¡£¾Ù¸öÀý×Ó£¬ÎÒÃÇÓµÓÐÒ»¸öÓû§±í£¬ÄÚÀï°üÀ¨id¡¢Óû§ÃûºÍÃÜÂë¡£ÒªÅÌÎÊËùÓÐÓû§µÄÓû§Ãû£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
// ½¨ÉèÒ»¸öUserÄ£×ÓʵÀý $user = new \app\model\User(); // ÅÌÎÊËùÓÐÓû§µÄÓû§Ãû $usernames = $user->field('username')->select(); // ´òÓ¡Óû§Ãû foreach ($usernames as $username) { echo $username['username'] . "<br/>"; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öUserÄ£×ÓʵÀý¡£È»ºó£¬ÎÒÃÇʹÓÃfieldÒªÁìÖ¸¶¨ÎÒÃÇÒªÅÌÎʵÄ×Ö¶ÎÃû¡£×îºó£¬ÎÒÃÇʹÓÃselectÒªÁìÅÌÎÊÖ¸¶¨×ֶεÄÖµ¡£ÔÚforeachÑ»·ÖУ¬ÎÒÃÇ´òÓ¡ÁËÿ¸öÓû§Ãû¡£
µÚÈý²½£ºÅÌÎʵ¥¸ö¼Í¼ÖÐÖ¸¶¨×ֶεÄÖµ
ÓÐʱ¼ä£¬ÎÒÃÇÖ»ÐèÒªÅÌÎʵ¥¸ö¼Í¼ÖÐÖ¸¶¨×ֶεÄÖµ¡£ÎÒÃÇ¿ÉÒÔʹÓÃfindÒªÁìÀ´ÊµÏÖÕâ¸ö²Ù×÷¡£ÀýÈ磬ÎÒÃÇÒªÅÌÎÊidΪ1µÄÓû§µÄÓû§Ãû£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
// ½¨ÉèÒ»¸öUserÄ£×ÓʵÀý $user = new \app\model\User(); // ÅÌÎÊidΪ1µÄÓû§µÄÓû§Ãû $username = $user->where('id', 1)->value('username'); // ´òÓ¡Óû§Ãû echo $username;
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÊ×ÏȽ¨ÉèÁËÒ»¸öUserÄ£×ÓʵÀý¡£È»ºó£¬ÎÒÃÇʹÓÃwhereÒªÁìÖ¸¶¨id¼´ÊÇ1µÄ¼Í¼¡£×îºó£¬ÎÒÃÇʹÓÃvalueÒªÁì»ñÈ¡Óû§Ãû×ֶεÄÖµ¡£
µÚËIJ½£ºÏÞÖÆÅÌÎÊЧ¹ûÊýÄ¿
ͨ³££¬ÎÒÃDz»ÐèÒªÅÌÎÊËùÓмͼÖеÄÖ¸¶¨×ֶΡ£ÎÒÃÇ¿ÉÒÔʹÓÃlimitÒªÁìÀ´ÏÞÖÆÅÌÎÊЧ¹ûÊýÄ¿¡£ÀýÈ磬ÎÒÃÇÖ»ÏëÅÌÎÊÇ°10¸öÓû§µÄÓû§Ãû£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
// ½¨ÉèÒ»¸öUserÄ£×ÓʵÀý $user = new \app\model\User(); // ÅÌÎÊÇ°10¸öÓû§µÄÓû§Ãû $usernames = $user->field('username')->limit(10)->select(); // ´òÓ¡Óû§Ãû foreach ($usernames as $username) { echo $username['username'] . "<br/>"; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃlimitÒªÁìÖ¸¶¨ÅÌÎÊЧ¹ûÊýĿΪ10¡£
ÒÔÉϾÍÊÇthinkphpÅÌÎÊÖ¸¶¨×Ö¶ÎÖµµÄÒªÁìÊÇʲôµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡