Thursday, 17 November 2016

php - mysql_query() Access denied for user



I'm making a PHP and MySQL RPG game, and my changeEquipment page is not working. I'm getting these errors:




Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'@'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21




Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21



Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'@'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27



Warning: mysql_query() [function.mysql-query]: A link to the server

could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27




Here's my code:



// 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);
?>



So, the PHP should be connecting to my databse using the devinfa1_user username and dbpass password, but it's apparently trying to use the devinfa1 username with no password.



I have no idea why this is the case, because I have the exact same connection code and an update statement on my changeHero page and it works perfectly. Any ideas?


Answer



Pass the connection in with the query, per the manual : The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.



see if that works, it looks as if is overriding the last connection (ie in the call to hero_has_multiple_weapons() a db connection is opened) or your last connection didnt really connect


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...