www.webdeveloper.com
Results 1 to 11 of 11

Thread: MySQL: Logging in as wrong user?

  1. #1
    Join Date
    Jul 2011
    Posts
    54

    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:

    PHP Code:
    <?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);
    ?>
    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:

    PHP Code:
    Could not connectAccess denied for user 'www-data'@'localhost' (using passwordNO
    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2011
    Posts
    54
    I have setup an echo statement to print the values of the global variables, and they are properly set and initialized.

    any other suggestions?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    How are you confirming the values? Something like this?
    PHP Code:
    <?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());
        }
    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 mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2011
    Posts
    54
    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.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Uh-oh, sounds like somebody turned on sql.safe_mode on the web host.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2011
    Posts
    54
    So how do I go about turning it off?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    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().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jul 2011
    Posts
    54
    Do you know the default location on linux for the php.ini file? I have server access.

  10. #10
    Join Date
    May 2012
    Posts
    42
    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:
    PHP Code:
    <?php 
        $con 
    mysql_connect($GLOBALS["dbHost"], $GLOBALS["dbUser"], $GLOBALS["dbPass"], true);
    ?>
    And if it works ... you know what the issue is.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Quote Originally Posted by DaveRich View Post
    Do you know the default location on linux for the php.ini file? I have server access.
    This should tell you (look for the "Loaded Configuration File" line):
    PHP Code:
    <?php
    phpinfo
    (INFO_GENERAL);
    Last edited by NogDog; 05-28-2012 at 09:47 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles