#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    The Netherlands
    Posts
    12
    Rep Power
    0

    An error occured: Access denied for user 'a5148864'@'localhost' (using password: NO)


    Hello,


    This very simple piece of code already fails, I was looking for solutions but none of them helped me so far. This is what I have:

    PHP Code:
    $host "mysql6.000webhost.com";
    $user "a5148864_wos";
    $password "password";
    $database "a5148864_wos";

    function 
    connect()
    {
    $query "mysql_connect($host$user$password)";
    mysql_query($query) or die('An error occured: 'mysql_error());
    //mysql_select_db($database);

    }

    connect(); 
    It is supposed to connect, but instead I receive this message:

    arning: mysql_query() [function.mysql-query]: Access denied for user 'a5148864'@'localhost' (using password: NO) in /home/a5148864/public_html/functions.php on line 10

    Free Web Hosting

    PHP Error Message

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

    Free Web Hosting
    An error occured: Access denied for user 'a5148864'@'localhost' (using password: NO)

    I hope somebody can help me with this issue.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I have no idea what you're trying to do there. mysql_connect() is a PHP function. You have to call it in PHP, not wrap it in a string and treat it like a query.

    Unfortunately, you ignored my last reply:

    Originally Posted by Jacques1
    Check out this tutorial on PDO which explains how to do this securely.
    That mysql_* stuff you're fumbling with is obsolete since almost a decade and is about to be kicked out of PHP completely. What you want is PDO or MySQLi.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Thread moved to PHP.

    You're not calling this function properly. Even if you were, PDO is more correct/secure.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    The Netherlands
    Posts
    12
    Rep Power
    0
    Okay I tried that, and I got this error:

    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'a5148864'@'localhost' (using password: NO)' in /home/a5148864/public_html/functions.php:10 Stack trace: #0 /home/a5148864/public_html/functions.php(10): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 /home/a5148864/public_html/functions.php(21): connect() #2 {main} thrown in /home/a5148864/public_html/functions.php on line 10

    With this code:

    PHP Code:
    $dsn 'mysql:host=' $host ';dbname=' $database '&charset=utf8';
        
    $db = new PDO($dsn$user$password); 
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    To finally address the actual problem that brought you here,

    The username and/or password is wrong. Apparently you're not giving a password at all, which could easily be the problem. Are you sure $username and $password have the values you expect? They're currently null so probably not. What's the rest of your code like now?

    Comments on this post

    • Northie agrees
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    The Netherlands
    Posts
    12
    Rep Power
    0
    They should have a value in them.

    PHP Code:
    $host "mysql6.000webhost.com";
    $user "a5148864_wos";
    $password "testtest1";
    $database "a5148864_wos";

    function 
    connect()
    {
        
    $dsn 'mysql:host=' $host ';dbname=' $database '&charset=utf8';
        
    $db = new PDO($dsn$user$password);
        array(
    PDO::ATTR_EMULATE_PREPARES => false);

    /*
    $connect = mysql_connect($host, $user, $password);
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($database);*/

    }

    connect(); 
  12. #7
  13. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    They don't, because variables from outside functions are not available inside functions.
    PHP Code:
    function connect() 

        
    $host "mysql6.000webhost.com"
        
    $user "a5148864_wos"
        
    $password "testtest1"
        
    $database "a5148864_wos"

        
    $dsn 'mysql:host=' $host ';dbname=' $database '&charset=utf8'
        
    $db = new PDO($dsn$user$password); 
        array(
    PDO::ATTR_EMULATE_PREPARES => false); 
    Or find another way to deal with those values.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    PHP functions cannot access outside variables(*). If you want to pass values to a function, you need to use its parameters.

    However, in this case you really shouldn't use variables at all -- at least not global variables. I mean, clearly you don't want to reuse your database credentials later in the code. They're not supposed to show up anywhere except in the PDO constructor. So why not write them directly into the constructor or the connect() function?



    (*) To be perfectly fair: PHP has a loophole for breaking this rule in case of global variables. You can import global variables through the $GLOBALS array or the global keyword. But you should generally avoid this, because it's very confusing.

    Newer versions of PHP also have closures like JavaScript. But this is a rather exotic feature you shouldn't use for everyday tasks.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    The Netherlands
    Posts
    12
    Rep Power
    0
    Yet again; thank you for helping me. It works now.
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    To explain the error more, and addressing requinix's post: You were trying to use mysql_query without calling mysql_connect. A call to mysql_query without an active database resource present will first try to connect to the database with the credentials listed in php.ini. Your credentials in PHP.ini are wrong (note the username in the error differs from the one in your code).

    However, the error is a red herring since the REAL actual problem is that you were doing everything wrong, not the particular subset of the problem where the error message was talking about a password. That doesn't matter, since you weren't connecting right in the first place.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo