Home >Backend Development >PHP Tutorial >php framework - thinkPHP What is the difference between these two methods of configuring M?

php framework - thinkPHP What is the difference between these two methods of configuring M?

WBOY
WBOYOriginal
2016-07-06 13:53:38982browse

<code>//第一种,在 *Model.class.php:
protected $dbName = 'ybdbshop';    //库名,也可以从配置文件,或者 D() 参数设置;
protected $tableName = 'abc';    //不加前缀的表名,也可以从类名设计;
protected $tablePrefix="";    //表前缀;
</code>
<code>//第二种:
protected $connection = array
(
    'DB_TYPE' => 'mysql',
    'DB_USER' => 'root',
    'DB_PWD'  => '',
    'DB_HOST' => 'localhost',
    'DB_PORT' => '3306',
    'DB_NAME' => 'ybdbcjd',    //库名
    'DB_CHARSET' =>    'UTF8',
);</code>

What is the difference between the two?
Why does the first one sometimes work without error, but when I copy the entire project to another computer, it doesn’t work?

Reply content:

<code>//第一种,在 *Model.class.php:
protected $dbName = 'ybdbshop';    //库名,也可以从配置文件,或者 D() 参数设置;
protected $tableName = 'abc';    //不加前缀的表名,也可以从类名设计;
protected $tablePrefix="";    //表前缀;
</code>
<code>//第二种:
protected $connection = array
(
    'DB_TYPE' => 'mysql',
    'DB_USER' => 'root',
    'DB_PWD'  => '',
    'DB_HOST' => 'localhost',
    'DB_PORT' => '3306',
    'DB_NAME' => 'ybdbcjd',    //库名
    'DB_CHARSET' =>    'UTF8',
);</code>

What is the difference between the two?
Why does the first one sometimes work without error, but when I copy the entire project to another computer, it doesn’t work?

I have already answered this question for you just now.

<code class="php">class classoneModel extends Model
{
    protected $dbName = 'ybdbcjd'; 
}</code>
What does the

in $dbName do?
It is used to operate another database under the same mysql account. If you do have this database in the same mysql account and have the corresponding permissions, it can be executed correctly. The generated similar sql statement is:

<code class="sql">select * from ybdbcjd.table</code>

This is not switching databases in the sense of thinkphp itself, but mysql's own cross-database operation syntax, which has great limitations and is not recommended.
is to operate other databases in the environment of the current database.

<code class="sql">use db1
select * from db2.table</code>

But the significance of the second direct modification $connection is to switch the database connection.
is to operate other databases under other databases.

<code class="sql">use db2
select * from tablez</code>

The difference is here.
If it is a small amount of operations, it can span databases. Otherwise, just create a new database connection directly.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn