thinkphpÔõôÅþÁ¬Êý¾Ý
thinkphpÊÇÒ»¸öʹÓÃphp±àдµÄ¿ªÔ´web¿ò¼Ü£¬¾ßÓмòÆÓÒ×ѧ¡¢¸ßЧ¿ìËÙ¡¢Çå¾²Îȹ̵ÈÌص㡣ÔÚʹÓÃthinkphp¾ÙÐпª·¢Ê±£¬ÐèÒªÅþÁ¬Êý¾Ý¿âÀ´´æ´¢ºÍÖÎÀíÊý¾Ý¡£±¾ÎĽ«ÏÈÈÝÔõÑùÅþÁ¬Êý¾Ý¿â²¢¾ÙÐлù±¾µÄ²Ù×÷¡£
Ò»¡¢ÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢
ÔÚThinkPHPÖУ¬ÐèÒªÔÚ¹«¹²ÉèÖÃÎļþconfig.phpÖÐÉèÖÃÊý¾Ý¿âµÄÅþÁ¬ÐÅÏ¢¡£ÔÚconfig.phpÎļþÖУ¬ÕÒµ½ÒÔÏ´úÂ룺
//Êý¾Ý¿âÀàÐÍ 'DB_TYPE' => 'mysql', //ЧÀÍÆ÷µØµã 'DB_HOST' => 'localhost', //Êý¾Ý¿âÃû 'DB_NAME' => 'thinkphp', //Óû§Ãû 'DB_USER' => 'root', //ÃÜÂë 'DB_PWD' => 'root', //¶Ë¿Ú 'DB_PORT' => '3306', //±íǰ׺ 'DB_PREFIX' => 'think_',
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬DB_TYPEΪÊý¾Ý¿âÀàÐÍ£¬ÏÖÔÚÖ§³Ömysql¡¢mysqli¡¢PDOµÈ¶àÖÖÀàÐÍ£»DB_HOSTΪÊý¾Ý¿âЧÀÍÆ÷µØµã£¬¿ÉÒÔÊÇIPµØµã»òÓòÃû£»DB_NAMEΪÊý¾Ý¿âÃû£¬ÐèÒªÔÚÅþÁ¬Ö®Ç°ÏȽ¨É裻DB_USERºÍDB_PWD»®·ÖΪÊý¾Ý¿âµÄÓû§ÃûºÍÃÜÂ룬ÐèÒªÓлá¼ûÊý¾Ý¿âµÄȨÏÞ£»DB_PORTΪÊý¾Ý¿âÅþÁ¬µÄ¶Ë¿Ú£¬Ä¬ÒÔΪ3306£»DB_PREFIXΪÊý¾Ý±íǰ׺£¬ÓÃÓÚ¶à¸öÓ¦Óù²ÓÃÊý¾Ý¿âʱ×èÖ¹±íÃû³åÍ»¡£
¶þ¡¢ÅþÁ¬Êý¾Ý¿â
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚÍê³ÉÊý¾Ý¿âÅþÁ¬ÐÅÏ¢µÄÉèÖÃÖ®ºó£¬¾Í¿ÉÒÔͨ¹ýʵÀý»¯Ò»¸öÊý¾Ý¿â¹¤¾ßÀ´ÅþÁ¬Êý¾Ý¿â¡£ÔÚThinkPHPÖУ¬Ê¹ÓÃDbÀàÀ´ÅþÁ¬ºÍ²Ù×÷Êý¾Ý¿â¡£ÀýÈ磺
use thinkDb; // ÅþÁ¬Êý¾Ý¿â $conn = Db::connect();
µÇ¼ºó¸´ÖÆ
ÈôÊÇÐèÒªÖ¸¶¨ÅþÁ¬µÄÊý¾Ý¿âÉèÖ㬿ÉÒÔÔÚconnectÒªÁìÖÐת´ïÒ»¸öÊý×é²ÎÊý£¬Êý×éÖаüÀ¨Êý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£ÀýÈ磺
$config = [ //Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', //ЧÀÍÆ÷µØµã 'hostname' => 'localhost', //Êý¾Ý¿âÃû 'database' => 'thinkphp', //Óû§Ãû 'username' => 'root', //ÃÜÂë 'password' => 'root', //¶Ë¿Ú 'hostport' => '', //±íǰ׺ 'prefix' => 'think_', ]; // ÅþÁ¬Êý¾Ý¿â $conn = Db::connect($config);
µÇ¼ºó¸´ÖÆ
Èý¡¢Êý¾Ý¿â²Ù×÷
ÅþÁ¬Êý¾Ý¿âÖ®ºó£¬¾Í¿ÉÒÔ¾ÙÐÐÊý¾Ý¿â²Ù×÷£¬°üÀ¨ÅÌÎÊ¡¢²åÈë¡¢¸üкÍɾ³ýµÈ²Ù×÷¡£ÏÂÃæÒÔÅÌÎʲÙ×÷ΪÀý¾ÙÐÐ˵Ã÷¡£
ÅÌÎÊÒ»Ìõ¼Í¼
ʹÓÃqueryÒªÁìÅÌÎÊÒ»Ìõ¼Í¼£¬ÀýÈ磺
// ÅÌÎÊÒ»Ìõ¼Í¼ $record = $conn->query('select * from think_user limit 1');
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬queryÒªÁì·µ»ØÒ»¸öPDOStatement¹¤¾ß£¬¿ÉÒÔͨ¹ýfetchÒªÁì»ñÈ¡Ò»Ìõ¼Í¼¡£
// »ñÈ¡ÅÌÎÊЧ¹û $row = $record->fetch(PDO::FETCH_ASSOC);
µÇ¼ºó¸´ÖÆ
ÅÌÎʶàÌõ¼Í¼
ʹÓÃselectÒªÁìÅÌÎʶàÌõ¼Í¼£¬ÀýÈ磺
// ÅÌÎʶàÌõ¼Í¼ $list = $conn->table('think_user')->select();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬tableÒªÁìÓÃÓÚÉèÖòÙ×÷µÄÊý¾Ý±í£¬selectÒªÁìÓÃÓÚ»ñÈ¡¶àÌõ¼Í¼¡£
Ìõ¼þÅÌÎÊ
¿ÉÒÔͨ¹ýwhereÒªÁìÉèÖÃÅÌÎÊÌõ¼þ£¬ÀýÈ磺
// Ìõ¼þÅÌÎÊ $list = $conn->table('think_user')->where('id', '=', 1)->select();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬whereÒªÁìÎüÊÕÈý¸ö²ÎÊý£¬»®·ÖΪ×Ö¶ÎÃû¡¢½ÏÁ¿ÔËËã·ûºÍÖµ£¬¿ÉÒÔͨ¹ýÁ´Ê½²Ù×÷ʵÏÖ¶à¸öÌõ¼þÅÌÎÊ¡£ÀýÈ磺
// ¶àÌõ¼þÅÌÎÊ $list = $conn->table('think_user') ->where('id', '=', 1) ->where('status', '=', 1) ->select();
µÇ¼ºó¸´ÖÆ
²åÈë¼Í¼
ʹÓÃinsertÒªÁì²åÈëÒ»Ìõ¼Í¼£¬ÀýÈ磺
// ²åÈë¼Í¼ $data = [ 'username' => 'admin', 'password' => md5('123456'), 'status' => 1, ]; $result = $conn->table('think_user')->insert($data);
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬insertÒªÁìÎüÊÕÒ»¸öÊý×é²ÎÊý£¬Êý×éÖаüÀ¨×Ö¶ÎÃûºÍ¶ÔÓ¦µÄÖµ¡£
¸üмͼ
ʹÓÃupdateÒªÁì¸üмͼ£¬ÀýÈ磺
// ¸üмͼ $data = ['status' => 0]; $result = $conn->table('think_user') ->where('id', '=', 1) ->update($data);
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬updateÒªÁìÎüÊÕÒ»¸öÊý×é²ÎÊý£¬Êý×éÖаüÀ¨Òª¸üеÄ×Ö¶ÎÃûºÍ¶ÔÓ¦µÄÖµ¡£
ɾ³ý¼Í¼
ʹÓÃdeleteÒªÁìɾ³ý¼Í¼£¬ÀýÈ磺
// ɾ³ý¼Í¼ $result = $conn->table('think_user') ->where('id', '=', 1) ->delete();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬deleteÒªÁì¿ÉÒÔɾ³ýÇкÏÌõ¼þµÄ¶àÌõ¼Í¼£¬²»×ª´ïÌõ¼þ²ÎÊýʱ»áÇå¿ÕÕûÕÅ±í¡£
ËÄ¡¢×ܽá
ͨ¹ýÉÏÊö°ì·¨¿ÉÒÔÀÖ³ÉÅþÁ¬Êý¾Ý¿â£¬²¢¾ÙÐлù±¾µÄÅÌÎÊ¡¢²åÈë¡¢¸üкÍɾ³ýµÈ²Ù×÷¡£ÔÚʹÓÃThinkPHP¾ÙÐпª·¢Ê±£¬ÐèÒª¾³£ÓëÊý¾Ý¿â´ò½»µÀ£¬Òò´ËÊìÁ·ÕÆÎÕÊý¾Ý¿â²Ù×÷ÊǺÜÊÇÖ÷ÒªµÄ¡£Í¬Ê±£¬ÔÚ¾ÙÐвÙ×÷ʱÐèҪעÖØÌá·ÀSQL×¢ÈëµÈÇå¾²ÎÊÌ⣬¾¡¿ÉÄÜʹÓòÎÊý°ó¶¨µÈ·½·¨À´×èֹΣº¦¡£
ÒÔÉϾÍÊÇthinkphpÔõôÅþÁ¬Êý¾ÝµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡