SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php/mysql question

    i try connect to a mysql database using the function mysql_connect("localhost","username","userpass") but i get this error: Access denied for user: 'root@localhost' (Using password: NO)

    why the php is trying to connect with the user "root" and without password since username is other than root and i specify a password (for username) ?

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure it's not also trying to connect elsewhere? Maybe post your full code too

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the full code:


    PHP Code:
    <?php

    $dbname
    ='aaa';

    function 
    print_error($msg) {
    echo 
    "<font face='verdana' size='3'><b>Error:</b><br><font size='2'>$msg</font></font>";
    exit;
    }

      
    $link_id_1 mysql_connect("localhost",'username','userpass') or print_error(mysql_error());
    mysql_select_db ($dbname);
    $result=mysql_query("SELECT * FROM `aaa`",$link_id_1);
     while(
    $row mysql_fetch_row($result)){

    echo(
    "$row[0]<br>");
     }
    ?>

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, if you only have one connection then you don't need to specify the link resource, so maybe try without it?
    PHP Code:
    mysql_connect('localhost','username','userpass') or print_error(mysql_error());
    mysql_select_db ($dbname) or print_error(mysql_error());
    $result=mysql_query("SELECT * FROM `aaa`",$link_id_1) or print_error(mysql_error());
    while(
    $row mysql_fetch_row($result)){

    echo(
    "$row[0]<br>");

    Also if username and userpass are actually variables, eg $username and $userpass then don't use single quotes.
    mysql_connect('localhost',$username, $userpass)

    My money's on the latter being the problem

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    username and userpass are not variables..
    also i've tryesd:

    $user="username";
    $pass="userpass";
    mysql_connect('localhost',$user, $pass)


    the problem is that the php is not sending the values user+pass to mysql (since is trying to connect with root and no pass instead username+userpass)

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem is that the php is not sending the values user+pass to mysql
    Right, but from what you've pasted that seems almost impossible. You can reproduce the error with code like:
    mysql_connect('localhost', '', '');

    I can't see how your code would produce that error. The error will also show a file and line number, is it matching the code/file you've pasted above?

  7. #7
    SitePoint Zealot stdlist's Avatar
    Join Date
    Sep 2004
    Location
    Corvallis, OR, USA
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The php code looks correct. I would think your issue is more likely a username/password mismatch or possibly a permission setting within MySQL.

    Double check the settings using the mysql utility to validate your settings then update the php code.

  8. #8
    SitePoint Member ruddhim's Avatar
    Join Date
    Mar 2005
    Location
    India
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jalex , I wud like to add to wht stdlist has said.
    Ensure that u have done FLUSH-PRIVILEGES after setting the new password in mysql DB
    Cos w\o that u cannot access other passwords (apart from ROOT)

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ruddhim
    jalex , I wud like to add to wht stdlist has said.
    Ensure that u have done FLUSH-PRIVILEGES after setting the new password in mysql DB
    Cos w\o that u cannot access other passwords (apart from ROOT)

    i've added the user ,that i try to make the connection, from webmin and i gived him all the rights

  10. #10
    SitePoint Member ruddhim's Avatar
    Join Date
    Mar 2005
    Location
    India
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not very sure about WEBMIN functionalities
    However jalex , giving the rights is one thing.
    But in order to activate the user account , one needs to use FLUSH-PRIVILEGES at mysql prompt(Just try that)

    Also , check out your table "user" in DB "mysql" (at myqsl prompt) and see whether it has the new entry ( u added via webmin ) with all flags set to 'Y'

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem in not from mysql, mysql username or password becouse i can connect to the database with these username and password from ssh command line (mysql -u username -p userpass) and also i can connect and make opperations on the database from a cgi script..

    so, the problem is from php or php-mysql module

  12. #12
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried a simple test script (outside of your current code) that just tests the connection/user/pass with mysql_connect() ? Do you get the same error with a hardcoded test script?

  13. #13
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    Have you tried a simple test script (outside of your current code) that just tests the connection/user/pass with mysql_connect() ? Do you get the same error with a hardcoded test script?

    yes, same error

  14. #14
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like a bug to me. What are you running on, windows, *nix? What version of PHP are you using and what version of MySQL?

  15. #15
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fedora core 2, php version 4.3.4 mysql i'm not sure what version is.. how can i see the version of mysql?

  16. #16
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a few bug reports relating to the problem you are having on http://bugs.php.net
    The first thing to do is upgrade PHP to the latest stable version, 4.3.10 just to rule out any old PHP bugs that may have since been fixed.
    If that doesn't fix it then the problem may be with the mysql library itself, search the bugs and see if anything suggested there helps. (but people have had the exact same problem as you before, seems like upgrading your 'stuff' in general is the best solution )
    Last edited by markl999; Mar 21, 2005 at 09:45.

  17. #17
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    There's a few bug reports realting to the problem you are having on http://bugs.php.net
    The first thing to do is upgrade PHP to the latest stable version, 4.3.10 just to rule out any old PHP bugs that may have since been fixed.
    If that doesn't fix it then the problem may be with the mysql library itself, search the bugs and see if anything suggested there helps. (but people have had the exact same problem as you before, seems like upgrading your 'stuff' in general is the best solution )

    if i upgrade the php i have to install again php-mysql modules?

  18. #18
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, best to redo the lot just to be sure. I presume you installed using packages and not from source? So the upgrade should be pretty painless.

  19. #19
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    ca
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    yeah, best to redo the lot just to be sure. I presume you installed using packages and not from source? So the upgrade should be pretty painless.

    ok.. i've upgrated to php 4.3.10 and now is working..
    i've spent 2 days trying to solve this problem

    thank you very much markl999


Bookmarks

Posting Permissions

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