2009-11-12 73 views
0

我已經設置兩個數據庫conections如下PHP多個數據庫問題

$con1 = mysql_connect("localhost", "root", "pwd") or die (mysql_error()); 

$con2 = mysql_connect("localhost", "wordpress", "pwd", true) or die(mysql_error()); 

mysql_select_db("lab_ancp", $con1) or die(mysql_error()); 
mysql_select_db("wordpress",$con2) or die(mysql_error()); 

,它工作正常

於是我做了一些查詢的網頁上是這樣的:

$sql="select unome from associado where uid=$uid"; 
    $result=mysql_query($sql,$con1) or die(mysql_error()); 

它工作正常,之後,我做這樣的第二個查詢:

$sql="select ID, post_content, post_title, post_excerpt, meta_value 
        from wp_posts join (
         select post_id, meta_value 
         from wp_postmeta 
          join (
           select post_id from wp_postmeta 
           where meta_key='destaque' and meta_value='s' 
          )as t1 using(post_id) 
         where meta_key='pft_widescreen' 
        ) as t2 on (wp_posts.ID=t2.post_id) 
       ORDER BY RAND() LIMIT 1"; 
      //echo $sql . "<br />"; 
      $row=mysql_fetch_assoc(mysql_query($sql,$con2)) or die(mysql_error()); 

,並再次一切都只是罰款,但後來....

$sql="select * from eventos where edatade>='$hoje' or edataate>='$hoje'"; 
      $result=mysql_query($sql, $con1) or die (mysql_error()); 

給出了這樣的錯誤:

**

SELECT command denied to user 'wordpress'@'localhost' for table 'eventos'

**

迫切需要幫助.. 。

回答

0

好吧

其解決了。

不要問我的原因,但我試圖改變前兩個roww的順序,即把$ con2放在$ con1之前,查詢現在可以正常工作。

我懷疑...「真」參數與此有關。

Thx guys。

+0

你應該編輯你的問題並選擇一個解決方案。如果您找到解決方案,請回答您自己的問題並選擇它。 – 2009-11-13 16:14:47

0

從錯誤它似乎你應該驗證權限的窩rdpress用戶在eventos表上。你的代碼似乎是正確的。

如果你想驗證這一點,也許嘗試使用第二個連接「SELECT * from eventos」。將其作爲腳本中的第一個查詢。

+0

他在最後一個查詢中使用$ con1,女巫使用root @ localhost,而不是wordpress @ localhost,女巫是$ con2 – fredrik 2009-11-12 23:34:58

0

http://se2.php.net/manual/en/function.mysql-select-db.php#39095 http://se2.php.net/manual/en/function.mysql-select-db.php#93487

似乎是通過mysql_select_db一個問題,第二連桿是一種解決方案。

我會推薦使用phps mysqli(MySQL Improved Extension)而不是老的mysql的東西(不知道它是否解決了你的問題,但它解決了你可能會遇到的其他問題)。

+0

thx fredrik,但我已經找到了解決方案。見下文。 – 2009-11-13 05:08:29

0

我懷疑原因是連接仍在內存中,因此當您嘗試執行第二個查詢時,這使用最後一次連接。我有與Joomla兩個數據庫使用問題類似的問題。我正在尋找解決這個問題的方法,我認爲這是我們的代碼中缺少的規則。


HEY!我明白了,問題是當你創建第二個連接時,對於默認的PHP返回最後一個連接的相同引用。所以,如果你需要一個新的連接,你應該準備與$ new_link的mysql_connect一個爲真,這樣

$myconn = @mysql_connect($this->db2_host,$this->db2_user,$this->db2_pass, true); 

最後一個參數意味着你需要一個新的參考,而不是最後一個。你可以找到更多信息:

HEREHERE

希望它能幫助。