Thinkphp method to realize multi-database connection _php Example

Source: Internet
Author: User
Tags php template smarty template

thinkphp when implementing a connection to multiple data, it is only necessary to define the model if the database is in the same server:

Class Membersmodel extends model{
protected $trueTableName = ' members.members ';//database name. Table name (contains prefix)
}

Then it can be like D ("members"), so instantiate the model and manipulate it like a normal model.
But it turned out that his database was on two different servers, so the above method was not.
At this point, you need to use the majority of TP connection features.

In this respect, consult the official documentation to test and revise and come up with the following solutions:

To establish a multiple data connection, you first construct the database configuration parameters. However, if you set up a database configuration array each time you build a multiple database connection, it would be cumbersome to write in a configuration file. It's still a bit tricky to write here.

<?php $config = Array (' Debug_mode ' =>true, ' default_module ' => ' Index '), ' router_on ' =>true, ' data_result_  TYPE ' =>1, ' show_run_time ' =>true,//Run time display ' Show_adv_time ' =>true,//Show detailed run time ' show_db_times ' =>true,// Display database query and write count ' Show_cache_times ' =>true,//Show cache operation Count ' Show_use_mem ' =>true,//Show memory overhead ' html_file_suffix ' => '. Sht    ML ',//default static file suffix ' html_cache_on ' =>false,///default shutdown static cache ' Html_cache_time ' =>60,//Static cache validity ' Html_read_type ' =>1, Static cache read mode 0 ReadFile 1 redirect ' Html_url_suffix ' => '. shtml ',//pseudo static suffix settings//default database link ' db_type ' => ' mysql ', ' db_host ' => ' localhost ', ' db_name ' => ' news ', ' Db_user ' => ' root ', ' db_pwd ' => ' 123 ', ' Db_port ' => ' 3306 ', ' Db_prefix ' => ' News_ ',//My first database connection to ' Db_bbs ' =>array (' DBMS ' => ' MySQL ', ' username ' => ' discuz ', ' Password ' => ', ' ", ', ', ') ', '" 123 ', "H Ostname ' => ' localhost ', ' hostport ' => ' 3306 ', ' db ' => ' discuz '),//Second database link, ' db_news ' =>array (' DBMS ' = > ' mysql ', ' username ' => ' root ', ' pasSword ' => ' 123 ', ' hostname ' => ' localhost ', ' hostport ' => ' 3306 ', ' database ' => ' news '); return $config;?>

So we can use C ("Db_bbs") and C ("Db_news") to get the configuration array of the database.
Well configured, now you need to instantiate the model. Because we need to use two different database connections for this model, the project's configuration file defaults to a database configuration, if you create a table model such as UserModel.class.php,
If you use D ("User"), if you do not have a user table in the current default database, you will get an error. So we're going to build an empty model. An empty model does not select a table.
There are two ways to build an empty model. $dao =d () and $dao=new Model ();

$dao =d ();

After instantiating the model, we need to increase the database model;

$dao->addconnect (C ("Db_bbs"), 1,true);
$dao->addconnect (C ("Db_news"), 2,true);

Say this addconnect (); The prototype of this function is different from 1.0.3 and 1.0.4.
The prototype in 1.0.3 is:

Boolean addconnect (mixed $config, mixed $linkNum, [Boolean $eqType = True])

The prototype in 1.0.4 is:

Boolean addconnect (mixed $config, mixed $linkNum)

The third argument is missing.
The first parameter is the configuration array of the database, and the second parameter is the number of the added connection, which needs to be given a connection that is the serial number when switching the database connection. Note the built-in database connection serial number is 0, so the additional database connection number should start at 1. The third argument is true if two databases are the same connection;

Once you have added a database connection, you can switch the database connection at any time. For example, we're going to use db_news this database, and that's what it says:

$dao->switchconnect (2);

Because there is only a connection to the database and there are no tables selected, you need to select the table next.
Note that the table name here is the full name, which is the table prefix plus the table name. Because we have no prefix in the configuration array of the connection database. I think it should be defined, but I don't know. That's it now.

$dao->table ("Cdb_members");

The model can then be used like a normal model.
For example, I want to query all the information of the user who passed the ID:

$map =array ("id" =>$_get["id"]);
$res = $dao->find ($map);

You can see if the query succeeded.

Dump ($res);

If you want to use the Db_bbs database table now, just switch the connection again;

$dao->switchconnect (2);

Then select the table query again. Remember, you must select the table again after switching the model, or you will get an error.
It can then be manipulated like a normal model.
Here are some of the questions that the manual points out:

1. A non-empty model was established when instantiating multiple database connections. (It seems to have been wrongly written.) This can be an error. It is suggested to establish an empty model;
The parameters of 2.addConnect () are different in different versions and are not written in the manual;
3. You need to select a table after you have established an empty model, which is not available in this manual.

For the above points, thinkphp users can adjust according to the different version of the appropriate.

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.