php切换数据库的问题,在线等
PHP 数据库 行业数据
RT,我的目的是想通过一个地方改变数据库链接,其他页面查询的时候使用新的链接。。现在我进入一个查询页面tttt.php,用的默认链接,查询出数据为3
然后我通过页面下拉框选择其他区域,选择下拉框的时候我用ajax从后台切换了数据库的链接
这时候我再进入查询页面ttt.php,数据依然是上一个DB的数据,求破。。。
除了把每个区域的DB都列出来切换,请问还有没有其他比较好的方法?(因为区域是动态增加的)
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 "查出来的条数:".$res[0]['ct']."<br/>"; //$dbsingleHelper -> changeDbLink("xxxx", "xxx", "xxx", "xxx"); //$r = $dbsingleHelper -> doSql($sql); //echo "新条数:".$r[0]['ct'];
上图的代码,把注释打开是正确的,但是问题是我切换的动作是由操作者触发的,在另外一个页面,另外一个页面触发切换后,这个查询的结果依然是上个DB的数据。。。
意思就是我在一个地方切换db后,无法改变所有页面其他的地方。。。尽管用单列也不行
求指点。。。
回复讨论(解决方案)
既没有看到传入的数据库条件,也没有看到选择数据库
既没有看到传入的数据库条件,也没有看到选择数据库
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; }
我在切换的时候已经在类里选好了,但是查询的时候用此类的函数来查询,如果不是在当前页面调用changeDbLink,则仍然是跟没切换一样的结果
没有调用changeDbLink,当然就不能切换数据库了
没有调用changeDbLink,当然就不能切换数据库了 我在其他的地方调用changeDbLink不行吗???
在哪里调用都可以,只要是针对的是同一连接、并且是在查询前
在哪里调用都可以,只要是针对的是同一连接、并且是在查询前
刚去测试了,var_dump了mysql_connect的返回值
self::$m_con = mysql_connect($server, $user, $pwd);var_dump(self::$m_con);
其中$m_con是db类的静态变量,并且在每次查询的时候同样var_dump这个值。。
先运行查询页面tttt.php显示结果resource(6) of type (mysql link) 查出来的条数:5(查询的时候var_dump $m_con的值)
然后运行切换DBLINK的页面dbchange.php,显示resource(7) of type (mysql link)(重建链接打印的$m_con的值)
接着再运行tttt.php,结果还是resource(6) of type (mysql link),为什么呢?
tttt.php的代码
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 "查出来的条数:".$res[0]['ct']."<br/>";
changedb.php的代码
$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);
我发现我用的不是单例??????
我2个页面都用$dbsingleHelper = DBSingleHelper::singleton(),但是都打了flag??
public static function singleton() { if(!isset(self::$m_instance)) { echo "ssssssssssssssssssssssssssss"; $cls = __CLASS__; self::$m_instance = new $cls; } return self::$m_instance; }
查了下资料,说php变量都是页面级的,每次调用页面重建,页面执行完毕清理内存,这样我怎么保证我2个页面用的是同一个链接呢
2个页面通过url调度的话,就不可能用同一连接
程序结束,数据库也就关闭了
但是你可以通过序列化在两个页面间传递对象的现场
2个页面通过url调度的话,就不可能用同一连接
程序结束,数据库也就关闭了
但是你可以通过序列化在两个页面间传递对象的现场 能给个sample吗?不是很懂。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...
