2012-05-11 77 views
0

我正在製作PHP和MySQL RPG遊戲,而我的changeEquipment頁面無法正常工作。我得到這些錯誤:mysql_query()拒絕用戶訪問

警告:請求mysql_query()[function.mysql查詢]:在 /家/ devinfa1:拒絕訪問用戶 'devinfa1' @ 'localhost' 的(NO使用密碼) /public_html/spellsword/changeEquipment.php第21行

警告:請求mysql_query()[function.mysql查詢]:一個鏈接 不能在 /家要建立的服務器/ devinfa1 /的public_html/spellsword/changeEquipment .php on line 21

警告:mysql_query()[function.mysql-query]:拒絕訪問用戶01在 /home/devinfa1/public_html/spellsword/changeEquipment.php第27行

警告:'devinfa1' @ '本地主機'(NO使用密碼):的mysql_query()[function.mysql-查詢]:一個鏈接到 無法在 /home/devinfa1/public_html/spellsword/changeEquipment.php在線服務器建立27

這裏是我的代碼:

<?php 
// Get commonFunctions.php 
require_once('commonFunctions.php'); 

// Start the session and connect to the database 
session_start(); 
$con = mysql_connect("localhost","devinfa1_user","dbpass"); 
if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db("devinfa1_spellsword", $con); 

// If logged in 
if(isset($_SESSION['account_name'])) { 
    // If form is submitted 
    if(isset($_POST['submit']) || isset($_POST['submit_x'])) { 

     // If the hero has more than one weapon 
     if(hero_has_multiple_weapons()) { 
      // Set new weapon 
      mysql_query("UPDATE Hero_Weapons SET active_weapon = TRUE WHERE HW_hero_id = 
       (SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]') 
       AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_POST[weapon]')"); 

      // Unset previous weapon 
      if(isset($_SESSION['hero_name'])) { 
       mysql_query("UPDATE Hero_Weapons SET active_weapon = FALSE WHERE HW_hero_id = 
        (SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]') 
        AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_SESSION[weapon_name]')"); 
      } 
     } 

// Close the database connection 
mysql_close($con); 
?> 

因此,PHP應該被連接到我的數據庫使用devinfa1_user用戶名和dbpass密碼,但它顯然嘗試使用沒有密碼的devinfa1用戶名。

我不知道爲什麼會出現這種情況,因爲我在我的changeHero頁面上有完全相同的連接代碼和更新語句,並且它完美地工作。有任何想法嗎?

+1

是那裏的commonFunctions一些代碼.php是做任何數據庫的東西? – Dennis

+0

是的。 commonFunctions.php有英雄_has_multiple_weapons()函數 – DevinFalgoust

+0

看到我的答案,我相信這是你的問題所在的地方 – Dennis

回答

1

將連接傳遞給查詢,根據手冊:MySQL連接。如果未指定鏈接標識符,則假定由mysql_connect()打開的最後一個鏈接。如果沒有找到這樣的鏈接,它會嘗試創建一個,就好像調用mysql_connect()時沒有參數一樣。如果未找到或建立連接,則會生成E_WARNING級別錯誤。

其是否正常工作,它看起來好像是壓倒一切的最後一個連接(即調用hero_has_multiple_weapons()一個數據庫連接打開)或您的最後一個連接真正地沒有連接

+0

我想通了。我正在關閉hero_has_multiple_weapons函數末尾的連接。感謝您的幫助 – DevinFalgoust

+0

沒問題,在需要的時候幫助並獲得幫助。恭喜 – Dennis