Php database switching problems, online and so on

Source: Internet
Author: User
Tags php database
Php database switching problems, online and other PHP database industry data

RT, my goal is to change the database link in one place and use the new link when querying other pages ..
Now, I enter tttt. php on the query page and use the default link to query 3 data records.
Then, I select other regions from the drop-down list. when I select the drop-down list, I used ajax to switch the database link from the background.
At this time, I will go to the query page ttt. php. The data is still the data of the previous DB...

In addition to listing the databases in each region for switchover, is there any better method? (Because the region is dynamically increased)
Require_once dirname (_ FILE __). '/.. /common/DbSingleHelper. php '; require_once dirname (_ FILE __). '/.. /svr_yunwei_config.php '; $ dbsingleHelper = DBSingleHelper: singleton (); $ SQL = 'SELECT count (*) as ct from login_log '; $ res = $ dbsingleHelper-> doSql ($ SQL); echo "number of items found :". $ res [0] ['CT ']."
"; // $ DbsingleHelper-> changeDbLink (" xxxx "," xxx "); // $ r = $ dbsingleHelper-> doSql ($ SQL); // echo "new count :". $ r [0] ['CT '];

Is correct, but the problem is that the switch action is triggered by the operator. on another page, after the switch is triggered on another page, the result of this query is still the data of the previous DB...
It means that after I switch the db in one place, I cannot change the rest of the page... Even if a single column is used
Please advise...


Reply to discussion (solution)

Neither the input database conditions nor the selected database

Neither the input database conditions nor the selected database

Public function changeDbLink ($ server, $ user, $ pwd, $ db_name) {mysql_close (self: $ m_con); return $ this-> getConnection ($ server, $ user, $ pwd, $ db_name);} public function killConnetion () {mysql_close (self: $ m_con);} // public static function singleton () {if (! Isset (self ::$ m_instance) {$ cls = _ CLASS __; self ::$ m_instance = new $ cls;} return self ::$ m_instance ;} private function getConnection ($ server = DB_SERVER, $ user = DB_USERID, $ pwd = DB_PASSWORD, $ db_name = DB_CATALOG) {self: $ m_con = mysql_connect ($ server, $ user, $ pwd); if (self ::$ m_con = false) {ErrorLog: saveLog ("db connection falid! Server :". $ server. ", user :". $ user. ", password :". $ pwd); exit;} $ db_sel = mysql_select_db ($ db_name); if ($ db_sel = false) {ErrorLog: saveLog ("db can't select database ". $ dbname); exit;} mysql_query ("set names utf8"); return self: $ m_con;} public function doSql ($ SQL) {$ data_list = array (); $ res = $ this-> Excute ($ SQL); $ I = 0; while ($ row = mysql_fetch_array ($ res) {$ data_list [$ I] = $ row; $ I ++;} return $ data_list ;}

I have already selected the class during the switchover, but I use this type of function for query. if changeDbLink is not called on the current page, the result is still the same as that of No switchover.

If changeDbLink is not called, the database cannot be switched.

If changeDbLink is not called, the database cannot be switched. Can I call changeDbLink elsewhere ???

You can call the same connection whenever it is used and before the query

You can call the same connection whenever it is used and before the query
I just went to the test and var_dump the return value of mysql_connect.

self::$m_con = mysql_connect($server, $user, $pwd);var_dump(self::$m_con);

$ M_con is a static variable of the db class, and the value var_dump is the same during each query ..
Run tttt. php on the query page to display the result resource (6) of type (mysql link): 5 (the value of var_dump $ m_con during query)
Then run dbchange. php on the page for switching DBLINK to display resource (7) of type (mysql link) (reset the value of $ m_con printed by the link)
Then run tttt. php and the result is resource (6) of type (mysql link). why?
Tttt. php code
Require_once dirname (_ FILE __). '/.. /common/DbSingleHelper. php '; require_once dirname (_ FILE __). '/.. /svr_yunwei_config.php '; $ dbsingleHelper = DBSingleHelper: singleton (); $ SQL = 'SELECT count (*) as ct from login_log '; $ res = $ dbsingleHelper-> doSql ($ SQL); echo "number of items found :". $ res [0] ['CT ']."
";

Changedb. php code
$dbsingleHelper = DBSingleHelper::singleton();$site = $_REQUEST['site'];$ip = $svr_url[$site]["url"];if(substr($ip, 0, 4) == "http"){$ip = substr($ip, 7);}$dbname = "yunwei".$site;$flag = $dbsingleHelper -> changeDbLink($ip.":xx", "xxx", "xxx", $dbname);

I found that I am not using a singleton ??????
I used $ dbsingleHelper = DBSingleHelper: singleton () for both pages, but all of them were flags ??

public static function singleton(){if(!isset(self::$m_instance)){echo "ssssssssssssssssssssssssssss";$cls = __CLASS__;self::$m_instance = new $cls;}return self::$m_instance;}

I checked the information and said that php variables are page-level. every time the page is re-called, the page is cleared after execution. how can I ensure that the two pages use the same link?

If two pages are scheduled through a url, the same connection cannot be used.
When the program ends, the database is closed.

However, you can pass the object field between two pages through serialization.

If two pages are scheduled through a url, the same connection cannot be used.
When the program ends, the database is closed.

But can you provide a sample by serializing the field where the object is transmitted between two pages? Not very familiar ..

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.