mysql_connect function isn't working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hendor
    New Member
    • May 2009
    • 4

    mysql_connect function isn't working

    Hi all. I've recently set up Apache 2.2 with PHP 5.2 and MySQL 5.1. I played around with SQL a bit, and now I'm trying to access it with PHP. I currently have the code:

    Code:
    <?php
    
    # error_reporting(E_ALL);
    # ini_set('display_errors', true);
    
    echo "test";
    
    $link = mysql_connect('localhost', 'root', '[mypassword]');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    
    ?>
    When I run the above in Firefox with my correct password, all I get is "test", verifying to myself that PHP was working when the script ran. I've also verified that the username and password correctly start MySQL though the CLI, but I don't think that's the issue here. If they were incorrect, I should get the "Could not connect" message and the MySQL error, but I get no errors at all. This makes me think the mysql_connect function itself isn't working right.

    What possible mistakes could I be making? Thanks in advance.
  • jamwil
    New Member
    • May 2009
    • 13

    #2
    I'm certainly no expert, but it looks to me like your code is sound.
    Seems to me like php is not communicating with mySql... Which I'm not the person to ask how to fix.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      still the question remains, why there is no further output. you could try using a database abstraction layer (e.g. PDO). they are usually more responsive/informative when it comes to errors.

      Comment

      • Ciary
        Recognized Expert New Member
        • Apr 2009
        • 247

        #4
        try this and see what it outputs (if it outputs something). whats wrong is, if an sql error occurs, your phpcode wil stop. therefor you need to put the error-output on the same line as where you get the error.

        Code:
        $link = mysql_connect('localhost', 'root', '[mypassword]') or die(mysql_error($link));
        warnings are different, they wont stop the executing code

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by Ciary
          try this and see what it outputs (if it outputs something). whats wrong is, if an sql error occurs, your phpcode wil stop. therefor you need to put the error-output on the same line as where you get the error.

          Code:
          $link = mysql_connect('localhost', 'root', '[mypassword]') or die(mysql_error($link));
          warnings are different, they wont stop the executing code
          I'm not sure you are right there.
          Using "or die()" or a if statement, like he did, is pretty much the same thing.

          SQL errors don't affect the execution of the PHP code, other than the mysql_connect function returning FALSE rather than returning a resource.

          Could it be that the mysql extension isn't installed, and the mysql functions simply aren't there?
          Try using the function_exists function to see if the mysql_connect function really exists.

          Comment

          • Hendor
            New Member
            • May 2009
            • 4

            #6
            Thanks for your responses; I think Atli got it. I ran this script:
            Code:
            <?php
            if (function_exists('mysql_connect')) {
                echo "Good<br />\n";
            } else {
                echo "Bad.<br />\n";
            }
            ?>
            It returned "Bad", so the MySQL extension for PHP is not installed. Is this the kind of thing that PHP would exclude when I choose the Recommended settings during its installation? Seems like a pretty major component to be considered unimportant... I'll reinstall PHP fully and try again.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by Hendor
              Is this the kind of thing that PHP would exclude when I choose the Recommended settings during its installation? Seems like a pretty major component to be considered unimportant... I'll reinstall PHP fully and try again.
              The MySQL extension was compiled into PHP4, but they detached it in PHP5 and made it an optional extension.
              Not sure exactly why, but I bet they had their reasons :)

              If you are using the Windows installer, you don't have to completely reinstall it.
              Just go to the Add/Remove Programs, find PHP in the list and click modify. Then you should be able to go through the installation wizard again and check the extensions you want.

              Comment

              • Hendor
                New Member
                • May 2009
                • 4

                #8
                Ok, thanks. I've done that, restarted Apache and computer, but it's still not working. PHP seems to work just as before, but still returns "Bad" when I run that script.



                I've read a few things and looked through my php.ini. I found this:

                extension_dir=" C:\Program Files\PHP\ext"

                The phpinfo() function also returns that directory as the extension location. When I look in that PHP directory, there is no "ext" sub-directory, only ones called dev and PEAR. So PHP is looking for the mysql (and other) extensions in a non-existent folder...

                Two questions: Do you know where the PHP installer would have put the extensions during installation, if not where it says in the ini file. And, is it just php_mysqli.dll and libmysql.dll that is needed, or is there more? Thanks.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Not a solution to your current problem, but try out one of the nifty installers that have most web-development components bundled with them.

                  XAMPP

                  EasyPHP

                  Both very easy to set up, and they do most of the work for you.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by Hendor
                    Two questions: Do you know where the PHP installer would have put the extensions during installation, if not where it says in the ini file. And, is it just php_mysqli.dll and libmysql.dll that is needed, or is there more? Thanks.
                    The installer should have put php_mysql.dll into a ext directory under the PHP installation path.
                    The libmysql.dll should be directly under the PHP installation path.

                    There are only three things that you need to do to get the MySQL extension working.
                    1. Put php_mysql.dll into the directory specified in the extension_dir directive.
                      If the installer didn't provide the DLL you can download the ZIP package at the php.net download page and use the DLL from there.
                    2. Add/uncomment "extension=php_ mysql.dll" in your php.ini file.
                    3. Add the PHP installation dir to your path variable.
                      (See the instructions here)
                      Alternatively, you can just copy the libmysql.dll into the Windows directory, although I would at least try setting the path variable first.

                    Then restart the server and it should be working.

                    Edit:
                    I just realized you wrote php_mysqli.dll, rather than php_mysql.dll.

                    These are two different extensions: The old MySQL Extension and the new Improved MySQL Extension.

                    They are two very different things.

                    Comment

                    • Hendor
                      New Member
                      • May 2009
                      • 4

                      #11
                      Originally posted by Atli
                      Edit:
                      I just realized you wrote php_mysqli.dll, rather than php_mysql.dll.

                      These are two different extensions: The old MySQL Extension and the new Improved MySQL Extension.

                      They are two very different things.
                      I think that was just an extraordinarily unlikely typo. I had heard of both mysql and mysqli, and I knew I was looking for mysql. Or maybe I just copied and pasted it from somewhere without noticing the 'i'.

                      Thanks for the tips, I'm sure they would have worked, but after changing so many settings over the past few days, I figured that even if I had fixed the problem, one of my other changes would have messed it up and I wouldn't know that I had fixed it. An hour ago, I uninstalled everything in order to start over and then saw Markus' comment. I've now got easyPHP installed and all my earlier problems are fixed. Now I just wish that I had saved my site and database before I deleted the Apache and MySQL folders. *facepalm*

                      Thanks all for your help, much appreciated.

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by Hendor
                        Thanks for the tips, I'm sure they would have worked, but after changing so many settings over the past few days, I figured that even if I had fixed the problem, one of my other changes would have messed it up and I wouldn't know that I had fixed it. An hour ago, I uninstalled everything in order to start over and then saw Markus' comment. I've now got easyPHP installed and all my earlier problems are fixed.
                        Yeah, I've been there before. You change so much in blind hope that it'll fix something. And then, when you do fix the error, you've caused other errors, and then you decide to walk away from the computer before beating it.

                        Originally posted by Hendor
                        Now I just wish that I had saved my site and database before I deleted the Apache and MySQL folders. *facepalm*

                        Thanks all for your help, much appreciated.
                        Been there too. I've lost lots of stuff doing that; thankfully, it wasn't important stuff.

                        Mark.

                        Comment

                        • Atli
                          Recognized Expert Expert
                          • Nov 2006
                          • 5062

                          #13
                          Originally posted by Hendor
                          Now I just wish that I had saved my site and database before I deleted the Apache and MySQL folders. *facepalm*
                          I did the same thing yesterday :)
                          Lost and entire MSSQL database I spent the last 2 days building.

                          Anyways, I'm glad you've got your sever working now.

                          P.S.
                          You should try using Linux as a server someday.
                          On my Ubuntu test server this entire problem would have been solved by a single command:
                          Code:
                          $sudo apt-get install php5-mysql
                          Easy, right? :P

                          Comment

                          Working...