PHP wont connect to MySQL!
Results 1 to 4 of 4

Thread: PHP wont connect to MySQL!

  1. #1
    Senior Member
    Join Date
    Sep 2001
    Posts
    121

    PHP wont connect to MySQL!

    Alright I hope to make this very easy to follow and easy to answer... rofl! ha! I have now idea what's going on and I'm a little annoyed at it!

    First I didnt have the extensions directory stated correctly... so it wasnt loading the php_mysql extension... that's all cleared up and php sees mysql. (I didnt put a / AFTER the directory!)

    I had that problem before and i couldnt figure it out so i updated everything. I got the newest php the newest apache and the newest mysql (newest recommended not betas).

    The old MySQL was like 4.01 or something like that, it was a while ago and it didnt remove the windows service so i had to reinstall it and remove it from cmd... it was a process (trial and error and luck)

    I have no redundant services and both apache and mysql are running all the time and mysql is "ready for connections" but php wont connect.

    I'm using win 2k fyi.

    I'll just try to immitate my code - on a different computer right now

    <?
    mysql_connect("localhost", "root", "root"); //yeah yeah it's *insecure*
    mysql_select_db("gallery");
    $query = "select * from news";
    $results = mysql_query($query);
    echo $results;
    ?>

    I have a or die statement after the connect and after the select db... and it wont connect to the db... so it doesnt go any further.

    I think I gave you all the info you need... yep

    oh and php is running as a module if that matters.

  2. #2
    Super Moderator
    Know-it-All Master Beaver

    Join Date
    Jan 2003
    Posts
    3,914
    Hey Hey..

    Go into your mysql\bin directory and run this command.... mysql -u root -p and enter the password of root and make sure it connects... if it doesn't then you know where your problem lies...

    Peace,
    HT
    IT Blog: .:Computer Defense:.
    PnCHd (Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255
    If HTRegz' test works, you might also try the following:
    - Double check your password really is "root" as you have specified.
    - Ensure that "root" is allowed to log in on your MySQL installation.

    It might also be helpful to take a gander at the installation/configuration instructions for MySQL: http://dev.mysql.com/doc/
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  4. #4
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    /EDIT: I should have read your post more carefully,
    but I don't get the exact point where the error
    appears...
    ...
    You may check whether you have a firewall or so,
    not allowing a connection to ..err.. 3306 (tcp),
    even if local login.

    PHP

    A snippet of my code (should look similar to yours):

    Code:
      #---------------------------------------------
      #SETTINGS
      $connector = "localhost";
      $login= "root";
      $password= "root";
      $dbname= "gallery";
      #---------------------------------------------
    
    $db = mysql_connect($connector,$login,$password);
    
      if ($db == false){
          print "<p>myERROR: Connection to MySQL-Server failed.</p>\n";
          exit;
      }
    
      #CONNECT TO DB
    
      $result = mysql_select_db($dbname,$db); 
      if ($result == false){
          print "<p>myERROR: $dbname does not exist.</p>\n";
          exit;
      }
    Note the "bolded" line. It may help to add the connector to the
    mysql_select_db command. By default, mysql_select_db should take
    the latest link, but it never hurts to add some extra information.

    If this does not work, try

    Mysql
    Code:
    #general db test
    > mysql -u root -p #as suggested
    > use mysql;
    > select * from user;
    also try there
    Code:
    #specific db test
    > show databases; #does gallery exist at all?
    > use gallery;
    > show tables;
    If this command works, then it's a PHP issue for almost certain.
    And since the mysql_connect seems to work ...
    Otherwise, check, as also suggest, whether root really has permission
    to gallery - which would be strange if not using a default installation.

    Cheers!
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides