//第一种,在 *Model.class.php:protected $dbName = 'ybdbshop'; //库名,也可以从配置文件,或者 D() 参数设置;protected $tableName = 'abc'; //不加前缀的表名,也可以从类名设计;protected $tablePrefix=""; //表前缀;
//第二种:protected $connection = array( 'DB_TYPE' => 'mysql', 'DB_USER' => 'root', 'DB_PWD' => '', 'DB_HOST' => 'localhost', 'DB_PORT' => '3306', 'DB_NAME' => 'ybdbcjd', //库名 'DB_CHARSET' => 'UTF8',);
What's the difference between these two?
Why does the first kind of me sometimes use no mistakes, and I copy the whole project to another computer?
Reply content:
//第一种,在 *Model.class.php:protected $dbName = 'ybdbshop'; //库名,也可以从配置文件,或者 D() 参数设置;protected $tableName = 'abc'; //不加前缀的表名,也可以从类名设计;protected $tablePrefix=""; //表前缀;
//第二种:protected $connection = array( 'DB_TYPE' => 'mysql', 'DB_USER' => 'root', 'DB_PWD' => '', 'DB_HOST' => 'localhost', 'DB_PORT' => '3306', 'DB_NAME' => 'ybdbcjd', //库名 'DB_CHARSET' => 'UTF8',);
What's the difference between these two?
Why does the first kind of me sometimes use no mistakes, and I copy the whole project to another computer?
I have answered this question just now.
class classoneModel extends Model{ protected $dbName = 'ybdbcjd'; }
What's in $dbName
it.
It is used to operate a different database under the same MySQL account, if you do have this database in the same MySQL account, and have the appropriate permissions, it can be executed correctly. The resulting SQL-like statement is:
select * from ybdbcjd.table
This is not thinkphp
its own sense of the switch database, but mysql
its own cross-database operation syntax, the limitations are large, is not recommended to use.
is to manipulate other databases in the context of the current database.
use db1select * from db2.table
But the second kind of direct modification $connection
is to switch the database connection.
is to operate another database under another database.
use db2select * from tablez
The difference is right here.
If it is a small number of operations can cross the database, or directly create a new database connection.