PHP switch Database issues, online, etc.

Source: Internet
Author: User
Tags database issues php database php switch
PHP Database Industry Data

RT, my aim is to use a new link when changing the database link in one place, and other page queries.
Now I go to a query page tttt.php, using the default link, query out the data is 3
Then I use the page drop-down box to select another area, select the drop-down box I used AJAX to switch the database link from the background
At this time I went into the query page ttt.php, the data is still the last DB data, find break ...

In addition to the list of DB in each region to switch, is there any other better way? (Because the area 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 "found the number of strips:". $res [0][' CT ']. "
;//$dbsingleHelper-Changedblink ("xxxx", "xxx", "xxx", "xxx");//$r = $dbsingleHelper, Dosql ($sql);//echo "new article number : ". $r [0][' CT '];

Code to open the comment is correct, but the problem is that I switch the action is triggered by the operator, on another page, another page triggered the switch, the result of this query is still the last db of data ...
It means that after I switch db in one place, I can't change all the pages elsewhere ... Not even with a single column.
Ask for advice ...


Reply to discussion (solution)

Neither see the incoming database condition nor see the Select database

Neither see the incoming database condition nor see the Select 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); }//Single column method 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;            The 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 selected in the class when I switch, but the query with this kind of function to query, if not on the current page call Changedblink, it is still the same as the result of no switch

Without calling Changedblink, of course you can't switch databases.

Do not call Changedblink, of course, can not switch the database I call Changedblink in other places do not???

Where the call can be, as long as it is for the same connection, and is before the query

Where the call can be, as long as it is for the same connection, and is before the query
Just went to the test, var_dump the return value of mysql_connect.

Self:: $m _con = mysql_connect ($server, $user, $pwd); Var_dump (self:: $m _con);

Where $m_con is a static variable of the DB class, and the same value is var_dump for each query:
Run the query page first tttt.php display results resource (6) of type (MySQL link) found: 5 (var_dump $m _con value when querying)
Then run the Toggle Dblink page dbchange.php, show resource (7) of type (MySQL link) (the value of the $m_con for rebuilding links to print)
Then run tttt.php, the result is resource (6) of type (MySQL link), why?
Tttt.php's 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 "found the number of strips:". $res [0][' CT ']. "
";

Changedb.php's 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 single case??????
I use $dbsinglehelper = Dbsinglehelper::singleton () on all 2 pages, but they all hit flag??

public static function Singleton () {if (!isset (self:: $m _instance)) {echo "ssssssssssssssssssssssssssss"; $cls = __class_ _;self:: $m _instance = new $cls;} Return self:: $m _instance;}

Check the information, said PHP variables are page-level, each call page reconstruction, page execution finished cleaning memory, so how can I guarantee that I 2 pages with the same link?

2 pages dispatched via a URL, it is impossible to use the same connection
The program is finished and the database is closed.

But you can serialize the field of objects passing between two pages

2 pages dispatched via a URL, it is impossible to use the same connection
The program is finished and the database is closed.

But you can do this by serializing a field that passes objects between two pages. Can I give you a sample? Not very understanding.

  • 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.