/    Sign up×
Community /Pin to ProfileBookmark

MySQL: Logging in as wrong user?

Hello,

I know it is not the best practice but I am working on a site for a friend and I am trying to figure out a weird issue with PHP/MySQL. I try logging in as the user root (I know this is not recommended), and the root password, which are both stored in globals. I’ve had the following code which was working for several years, until one day it stopped work mysteriously.

I am trying the following code here:

[code=php]
<?php
$con = mysql_connect($GLOBALS[“dbHost”], $GLOBALS[“dbUser”], $GLOBALS[“dbPass”]);

if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db($GLOBALS[“dbName”]);
mysql_query(“SET NAMES ‘utf8’ COLLATE ‘utf8_general_ci'”);

$sql=”SELECT * FROM `householdinfo_” . $year . “` WHERE geography =’99’ && subgeography ='” . $subgeoStr. “‘ && familytype LIKE'” . $famTypeStr . “%'”;

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
echo $row[‘geography’] . “;” . $row[‘subgeography’] . “;” . $row[‘familytype’] . “;” . $row[‘familycount’] . “;” . $row[‘medianincome’];
}

mysql_close($con);
?>
[/code]

I have double checked the contents of the globals, and the username/password/database defined in the globals are correct.

Howevere I get the following error:

[code=php]
Could not connect: Access denied for user ‘www-data’@’localhost’ (using password: NO)
[/code]

Is there any reason why when I try logging in as “root” it returns an error saying that I am trying to login as www-data without a password?

I have googled this for quite some time and the general result is either that I have run out of available connections from another PHP script not closing the mysql connecetion properly. However I have been unable to verify if that’s the case here.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 28.2012 — My guess is that at that point in the code, the variables you are using do not have any values, so it is trying to connect using the user name that is running your PHP script with no password. Thus one of the problems of depending on global variables. ?

But that's only a guess without doing some debugging to find out what the values are of those globals at that point in the code, then if not set (or wrong), tracing back through the code to see where they are defined or overwritten/unset.
Copy linkTweet thisAlerts:
@DaveRichauthorMay 28.2012 — I have setup an echo statement to print the values of the global variables, and they are properly set and initialized.

? any other suggestions?
Copy linkTweet thisAlerts:
@NogDogMay 28.2012 — How are you confirming the values? Something like this?
[code=php]
<?php
$con = mysql_connect($GLOBALS["dbHost"], $GLOBALS["dbUser"], $GLOBALS["dbPass"]);

if (!$con)
{
echo "host: '".$GLOBALS['dbHost']."'<br />n";
echo "user: '".$GLOBALS['dbUser']."'<br />n";
echo "pass: '".$GLOBALS['dbPass']."'<br />n";
die('Could not connect: ' . mysql_error());
}
[/code]

It certainly sounds like they are empty, since the default values come from the PHP config, which would explain the user name if $GLOBALS['dbUser'] were empty:

resource [B]mysql_connect [/B]([ string $server = ini_get("mysql.default_host") [, string $username = [COLOR="Red"]ini_get("mysql.default_user")[/COLOR] [, string $password = [COLOR="Red"]ini_get("mysql.default_password")[/COLOR] [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
Copy linkTweet thisAlerts:
@DaveRichauthorMay 28.2012 — That is how I was displaying them however I did the echo statement(s) both in the if statement and I also printed them out at the very top, before I tried establishing the connection.
Copy linkTweet thisAlerts:
@NogDogMay 28.2012 — Uh-oh, sounds like somebody turned on [url=http://www.php.net/manual/en/ini.core.php#ini.sql.safe-mode]sql.safe_mode[/url] on the web host. ?
Copy linkTweet thisAlerts:
@DaveRichauthorMay 28.2012 — So how do I go about turning it off?
Copy linkTweet thisAlerts:
@NogDogMay 28.2012 — Unless you have access to change the PHP system config (i.e. usually the php.ini file), I think you can override the values for the system defaults via ini_set().
Copy linkTweet thisAlerts:
@DaveRichauthorMay 28.2012 — Do you know the default location on linux for the php.ini file? I have server access.
Copy linkTweet thisAlerts:
@kbduvallMay 28.2012 — mysql_connect will automatically re-use any open connections if available. You can force it to create a new one by adding true to the 4th argument.

So try:
[code=php]
<?php
$con = mysql_connect($GLOBALS["dbHost"], $GLOBALS["dbUser"], $GLOBALS["dbPass"], true);
?>
[/code]

And if it works ... you know what the issue is.
Copy linkTweet thisAlerts:
@NogDogMay 29.2012 — Do you know the default location on linux for the php.ini file? I have server access.[/QUOTE]

This should tell you (look for the "Loaded Configuration File" line):
[code=php]
<?php
phpinfo(INFO_GENERAL);
[/code]
×

Success!

Help @DaveRich spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...