Connect to mySQL in php file?

Discussion in 'PHP' started by Aniviel, Jun 28, 2007.

  1. #1
    Yeah, I know there are countless tutes that tell me how. I am quite a noob, and I hope you can help me out.

    I've just been running through a tutorial to set up php/mysql/apache. I seem to be having a problem connecting to mySQL in a php file. MySQL seems to be working fine; I can create databases and tables through the command prompt client, but even this php test file can't seem to get past the line mysqli_connect("localhost", "root", "myPass", "myDB");. I have included the line "extension=php_mysqli.dll" in my php.ini file and set the extension_dir to the directory that does include that dll. The call to connect just seems to hang-- trying to echo something directly afterward produces no output. I have set max_execution_time to small numbers of seconds and waited for timeout, but I cannot get any errors. Can anyone suggest what I should look for next?

    Using...
    PHP 5
    MySQL Server 5.0

    Thanks for any advice.
     
    Aniviel, Jun 28, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    ansi, Jun 28, 2007 IP
  3. Aniviel

    Aniviel Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I have read the notes before, and even copying the sample code directly (and replacing the appropriate username/pass/server) gets me nowhere. Even if I am not using the correct parameters, shouldn't the connection just fail rather than hang like that? I can't even get an error message on die().

    Thanks!
     
    Aniviel, Jun 29, 2007 IP
  4. Aniviel

    Aniviel Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh, and someone in an irc chan suggested I use mysqli in case of compatibility issues with PHP5, but I have also tried mysql_connect with no success.
     
    Aniviel, Jun 29, 2007 IP
  5. Aniviel

    Aniviel Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The script works fine if I host it with a webhost that supports mysql. I really would like to develop on my own machine, though....
     
    Aniviel, Jun 29, 2007 IP
  6. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    then you will have to load the libmysql.dll library into the php core, and the php_mysql.dll extension.

    To load the libmysql.dll library, you have to make sure the PHP path is available in the windows Path system variable. If it's not available, add it, and reboot your pc. If the libmysql.dll file is not located in the php root folder, copy it in there.
    Then you have to open your php.ini file, and uncomment (by removing the semi-colon at the beginning of the line) the following line:
    ;extension=php_mysql.dll
    Code (markup):
    make sure the "extension_dir" points to the correct directory
    Then (re)start your server, to (re)load the php settings
     
    UnrealEd, Jun 29, 2007 IP
  7. Aniviel

    Aniviel Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    UnrealEd,
    These are things I had already done yesterday. Thanks for your help.

    Since I am a total noob at this stuff, please direct me in what direction my questions should be taking. I had originally assumed I was just sucking at php, but since I have tried numerous connection example scripts from reputable sources which all work fine -not- on my machine, this looks to be a different issue. Would it be more appropriate to seek help in a mySQL forum? (Even though your suggestions are much appreciated here!) Or is this inter-related enough to warrant a place in this forum?

    (On a side note, I am playing around with this stuff-- by relentlessly uploading junk to my web host, lulz-- and am surprised and pleased by its power. With the help of a couple of tutorials and much referencing of the php documentation, I have an ugly, bare-bones message board. Awesome.)
     
    Aniviel, Jun 29, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    try adding a die() to the connection. might give you a better idea of what is wrong.

    
    $conn = mysql_connect("localhost","username","password") or die( mysql_error() );
    mysql_select_db("database") or die( mysql_error() );
    
    PHP:
     
    ansi, Jun 29, 2007 IP
  9. einsteinsboi

    einsteinsboi Well-Known Member

    Messages:
    1,122
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    140
    #9
    1. What type of installation did you do for PHP? Did you use the msi installer or the the zip file?

    2. What error message do you get? Also try running phpinfo and see whether the page you get shows mySQL as being enabled. One way to do this is create a php file with the code below and run it:

    <?php
     phpinfo();
    ?>
    Code (markup):

     
    einsteinsboi, Jun 29, 2007 IP
  10. leighmilbourne

    leighmilbourne Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I am experiencing the exact same problem trying to connect to a mysql database.

    The code to connect is as follows:

    @ $db = mysql_connect($server, $username, $password) or die(mysql_error());

    Any code I have outputted before this statement is displayed but anything after it is not. I have an ifelse statement to test the $db handle but nothing displays. I have had a look at the phpinfo information but there is nothing about mysql in it. If anyone has a solution it would be greatly appreciated.

    Please email me on milbourne.leigh@gmail.com if you can help.

    Leigh.
     
    leighmilbourne, Aug 3, 2007 IP
  11. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #11
    http://www.apachefriends.org/en/xampp-windows.html

    Check out the following installation software. It comes prebundled with a lot of applications. MySQL server, Apahche, FTP, php, etc.. It will work straight out of the box most of the time and I recommend it 100% to everyone. I used it on my local computer. I think maybe you tried to install each thing seperate (you said command line mysql ouch!!) which can get confusing to setup.

    __________________
    Please, don't forget to give Rep [​IMG] is for great answers. :)
     
    exodus, Aug 3, 2007 IP
  12. Webray

    Webray Active Member

    Messages:
    469
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Not too long ago Windows and MySQL had password issues and I think this is the problem you're facing. Search for password related issues and MySQL. Also, try installing xampp, if you haven't already.
     
    Webray, Aug 3, 2007 IP