php切換資料庫的問題,線上等

來源:互聯網
上載者:User
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']."
";//$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']."
";

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嗎?不是很懂。。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.