How to connect to MySQL in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mathurinjerry
    New Member
    • Aug 2007
    • 2

    How to connect to MySQL in PHP

    I'm a newbie at this so if someone could please help me out:
    I have : PHP Version 5.2.3, Apache 2.2.4, and mySQL Version 4.1.22 on Windows XP.

    I have PHP working independantly. I have mySQL client working perfectly by itself. When trying to connect to the mySQL database, using PHP, i don't get anything: only a blank page. I need help! I've been struggling for 22hrs now trying to get it to work.

    This is what i've done:
    I created a user in mySQL client that has basic privalages on "sitename" database that i've created using the following:
    [code=mysql]
    GRANT SELECT, INSERT,UPDATE, DELETE ON sitename. * TO 'mathurinjerry' @'localhost' IDENTIFIED BY 'yellow83';

    FLUSH PRIVILEGES; //to apply changes
    [/code]

    Then when i tried to create a php script, and uploaded it in my server, all I get is a blank page.

    This is the script i used:
    [PHP]<?php
    echo mysql_connect ('localhost','m athurinjerry',' yellow83') or die('Cannot connect to database because:'.mysql _error());
    ?>[/PHP]

    Can someone please help me PLEASE!
    Last edited by Atli; Aug 25 '07, 11:20 PM. Reason: Added [code] tags and changed unnecessary bold text.
  • gregerly
    Recognized Expert New Member
    • Sep 2006
    • 192

    #2
    Originally posted by mathurinjerry
    I'm a newbie at this so if someone could please help me out:
    I have : PHP Version 5.2.3, Apache 2.2.4, and mySQL Version 4.1.22 on Windows XP.

    I have PHP working independantly. I have mySQL client working perfectly by itself. When trying to connect to the mySQL database, using PHP, i don't get anything: only a blank page. I need help! I've been struggling for 22hrs now trying to get it to work.

    This is what i've done:
    I created a user in mySQL client that has basic privalages on "sitename" database that i've created using the following:

    GRANT SELECT, INSERT,UPDATE, DELETE ON sitename. * TO 'mathurinjerry' @'localhost' IDENTIFIED BY 'yellow83';

    FLUSH PRIVILEGES; //to apply changes

    Then when i tried to create a php script, and uploaded it in my server, all I get is a blank page.

    This is the script i used:

    [PHP]<?php
    echo mysql_connect ('localhost','m athurinjerry',' yellow83') or die('Cannot connect to database because:'.mysql _error());
    ?>[/PHP]

    Can someone please help me PLEASE!
    By the looks of your script, I'de say you are probably connecting. The mysql_connect should be returning true or false depending on if it can connect or not, if not, it would echo "Cannot connect to database because...", so I would venture a guess that it is connecting.

    Try something more useful, like:

    [PHP]<?

    $con=mysql_conn ect('localhost' ,'mathurinjerry ','yellow83');

    if($con){
    echo "connected succesfully";
    }else{
    echo "could not connect to the database";
    }

    ?>[/PHP]
    This is essentially what you are doing with the die statement, but you are testing the value of $con which should be true if it connects.

    Also: Work on your post titles. Pbmods will be following promptly to tell you the same thing.

    Greg

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I have edited the thread's title to better describe it's contents.
      Please avoid using words like 'help me' when you name your threads, as they are
      less likely to attract the attention of our Experts, as well as our other members. Not to mention that other people facing similar problems will not be able to find it.

      Please take a look at the Posting Gudelines, specifically the part about How to use a good thread title, before posting in these forums.

      Moderator

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Jerry. Welcome to TSDN!

        If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          I think your Php is working fine in your host. is it?

          First try this to add these two lines to the top of the Php Pages. Some times error reporting might be disabled on the php.ini.
          [CODE=php]
          error_reporting (E_ALL);
          ini_set('displa y_errors', True);
          // Remaining Codes....
          [/CODE]

          [EDIT: Sorry Pbmods, this post is from a mirror thread, So the answer is same as your.]

          Comment

          • mathurinjerry
            New Member
            • Aug 2007
            • 2

            #6
            Originally posted by pbmods
            Heya, Jerry. Welcome to TSDN!

            If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
            i tried this and i still get nothing but a blank page:
            [PHP]
            <? error_reporting (E_ALL);
            ini_set('displa y_errors', TRUE);
            $con=mysql_conn ect('localhost' ,'__MUNGED__',' __MUNGED__');
            if($con){
            echo"connected successfully";
            }else{
            echo"could not connect to the database";
            }
            ?>

            [/PHP]
            Last edited by pbmods; Aug 27 '07, 05:01 PM. Reason: Removed bold, removed sensitive data.

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Jerry.

              Not a fan of whitespace, I see. If you're curious, developers have tested this, and whitespace does not affect script execution time to any significant degree.

              Out of curiosity, does this output anything:
              [code=php]
              <?php // Short tags are not universally supported.
              error_reporting (E_ALL);
              ini_set('displa y_errors', TRUE);
              /*
              $con=mysql_conn ect('localhost' ,'__MUNGED__',' __MUN GED__');
              if($con){
              echo"connected successfully";
              }else{
              echo"could not connect to the database";
              }
              */

              echo 'Well, at least this works....';
              ?>
              [/code]

              Comment

              • craigvansat
                New Member
                • Oct 2007
                • 1

                #8
                Did anyone ever get this figured out? I am having the same trouble on all my sites that connect to a mysql database ver 4.1 . I am with Server Beach and everything was running fine untill I had them upgrade my database to 4.1. I know my code should work but It's driving me mad and of course they are blaiming it on my code!

                Thanks All ~

                Craig

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Hi Craig. Welcome to TSDN!

                  If everything ran fine on an earlier version of MySQL your PHP code should be fine. It is possible, however, that your SQL queries are incompatible with your MySQL version.

                  Are you still using the same PHP version and configuration?
                  Have you checked out this article?

                  Could you post an example of the code that is not working?

                  Comment

                  Working...