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
Release: 2016-07-06 13:53:38
Original
970 people have browsed it

<code>//第一种,在 *Model.class.php:
protected $dbName = 'ybdbshop';    //库名,也可以从配置文件,或者 D() 参数设置;
protected $tableName = 'abc';    //不加前缀的表名,也可以从类名设计;
protected $tablePrefix="";    //表前缀;
</code>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

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>
Copy after login
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>
Copy after login

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>
Copy after login

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template