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?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:53:38
Original
1033 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.

class classoneModel extends Model
{
    protected $dbName = 'ybdbcjd'; 
}
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:

select * from ybdbcjd.table
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.

use db1
select * from db2.table
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.

use db2
select * from tablez
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:
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
Latest Issues
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
ThinkPHP Why use composer?
From 1970-01-01 08:00:00
0
0
0
thinkphp versions supported by php6
From 1970-01-01 08:00:00
0
0
0
thinkphp upload files
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template