thinkphp5Åжϱí×Ö¶ÎÊÇ·ñ±£´æ
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬web¿ª·¢±äµÃÔ½À´Ô½Ö÷Òª¡£ÓÉÓÚwebÓ¦ÓóÌÐòµÄÖØ´óÐÔ£¬web¿ª·¢µÄÄѶȺÍΣº¦Ò²ÔÚÔöÌí¡£ÎªÏàʶ¾öÕâЩÎÊÌ⣬ÓÐÐí¶àweb¿ò¼Ü±»¿ª·¢³öÀ´¡£¶øthinkphpÊÇÆäÖÐÒ»¸öºÜÊÇÊ¢ÐеÄweb¿ò¼Ü¡£ÔÚʹÓÃthinkphp¾ÙÐÐweb¿ª·¢Ê±£¬ÎÒÃǾ³£ÐèÒªÅжÏÊý¾Ý¿â±íÖеÄijЩ×Ö¶ÎÊÇ·ñ±£´æ¡£±¾ÎĽ«ÏÈÈÝʹÓÃthinkphp5À´Åжϱí×Ö¶ÎÊÇ·ñ±£´æ¡£
Êý¾Ý¿â²Ù×÷Àà
ThinkPHP5ÌṩÁËÒ»ÖÖºÜÊÇÀû±ãµÄÊý¾Ý¿â²Ù×÷Àࡪ¡ªDbÀࡣͨ¹ýʹÓÃDbÀ࣬ÎÒÃÇ¿ÉÒÔÀû±ãµØ¾ÙÐÐÊý¾Ý¿âµÄCRUD²Ù×÷¡£ÔÚʹÓÃDbÀàʱ£¬ÎÒÃÇͨ³£ÐèÒªÏÈÅþÁ¬Êý¾Ý¿â¡£ÅþÁ¬Êý¾Ý¿âµÄÒªÁìÈçÏ£º
use thinkDb; Db::connect($config);
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬$configÊÇÒ»¸öÊý×飬°üÀ¨Êý¾Ý¿âµÄÖÖÖÖÉèÖÃÐÅÏ¢£¬ÀýÈ磺
$config = [ 'type' => 'mysql', 'hostname' => 'localhost', 'database' => 'test', 'username' => 'root', 'password' => 'password', 'hostport' => '3306', 'charset' => 'utf8mb4', 'prefix' => '', 'debug' => true, ];
µÇ¼ºó¸´ÖÆ
ÅþÁ¬Àֳɺó£¬ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàµÄÖÖÖÖÒªÁìÀ´²Ù×÷Êý¾Ý¿â£¬ÀýÈçÅÌÎÊ£º
$result = Db::name('user')->where('id', 1)->find();
µÇ¼ºó¸´ÖÆ
Åжϱí×Ö¶ÎÊÇ·ñ±£´æ
ΪÁËÅжϱí×Ö¶ÎÊÇ·ñ±£´æ£¬ÎÒÃÇÐèÒªÅÌÎʱíµÄ½á¹¹ÐÅÏ¢¡£ÔÚMySQLÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃDESCÏÂÁ»òSHOW COLUMNSÏÂÁÀ´»ñÈ¡±íµÄ½á¹¹ÐÅÏ¢¡£ÔÚThinkPHP5ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàµÄgetTableFieldsÒªÁìÀ´»ñÈ¡±íµÄ×Ö¶ÎÐÅÏ¢£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$fields = Db::name('user')->getTableFields();
µÇ¼ºó¸´ÖÆ
getTableFieldsÒªÁì·µ»ØÒ»¸öÊý×飬°üÀ¨Á˱íÖеÄËùÓÐ×Ö¶ÎÃû¡£ÎÒÃÇ¿ÉÒÔÔÚÕâ¸öÊý×éÖвéÕÒÐèÒªµÄ×Ö¶ÎÃû¡£
$fields = Db::name('user')->getTableFields(); if (in_array($field, $fields)) { // ×ֶα£´æ } else { // ×ֶβ»±£´æ }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬$fieldÊÇÐèÒªÅÌÎʵÄ×Ö¶ÎÃû¡£
ÍêÕûµÄ´úÂëʾÀý
ÏÂÃæÊÇÒ»¸öÍêÕûµÄ´úÂëʾÀý£¬ÓÃÓÚÅжϱí×Ö¶ÎÊÇ·ñ±£´æ£º
use thinkDb; function fieldExists($table, $field) { $fields = Db::name($table)->getTableFields(); return in_array($field, $fields); } if (fieldExists('user', 'name')) { echo '×ֶα£´æ'; } else { echo '×ֶβ»±£´æ'; }
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚʹÓÃThinkPHP5¾ÙÐÐWeb¿ª·¢Ê±£¬¾³£ÐèÒªÅжÏÊý¾Ý¿â±íÖеÄijЩ×Ö¶ÎÊÇ·ñ±£´æ¡£ÎªÁËʵÏÖÕâ¸ö¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʹÓÃDbÀàµÄgetTableFieldsÒªÁìÀ´»ñÈ¡±íµÄ×Ö¶ÎÐÅÏ¢£¬È»ºóÅÌÎÊÐèÒªµÄ×Ö¶ÎÊÇ·ñ±£´æ¡£Ê¹ÓÃÕâÖÖÒªÁì¿ÉÒÔºÜÀû±ãµØÅжϱí×Ö¶ÎÊÇ·ñ±£´æ£¬×èÖ¹ÁËÔÚÅÌÎʱíÊý¾Ýʱ¿ÉÄÜ·ºÆðµÄ¹ýʧ¡£
ÒÔÉϾÍÊÇthinkphp5Åжϱí×Ö¶ÎÊÇ·ñ±£´æµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡