Code stops executing at mysql_connect(

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Sciberras
    New Member
    • Jan 2007
    • 4

    Code stops executing at mysql_connect(

    Hi All

    I am new to PHP and MySQL, so please go easy on me!

    The following is a simple PHP script to connect to my server. The MySQL service is running. The first echo statement displays, but the second one never displays. To me it implies that my connection to the database server is hanging, but I'm not sure what to look at. I don't even get an error, it just displays the first message and the second message never appears.

    <html>
    <head><title>Cr eate Database and table</title></head>
    <body>

    <?php

    echo "Before";
    $linkID = mysql_connect(" localhost","mar tin","martin") ;
    echo "After";

    ?>
    </body>
    </html>
  • IsraeliShaffer
    New Member
    • Jan 2007
    • 1

    #2
    Try changing the second parameter (username) "Robert" to "root".


    Shaffer.

    Comment

    • cassbiz
      New Member
      • Oct 2006
      • 202

      #3
      There are many good tutorials out there.



      In your above code you are asking to connect to a DB but only supplying the location, user and pass. Not asking for anymore, no query.

      Read up on some of the tutorials out there. I myself am new to PHP and MySQL and they have helped me loads.

      I still have errors and that is where this forum has helped tremendously.

      Good Luck

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Why do you post a MySQL question in the PHP forum?

        Before you give up or blame whatever, you should allow MySQL to give you a proper error message. Now you just say "it dies" and you don't even know why. If you would have included proper error handling, you would have received an error message stating the cause. So change your code into this[php]$linkID = mysql_connect(" localhost","mar tin","martin")
        or die("Could not connect to the database server: ".mysql_error() );
        mysql_select_db ("db_name",$lin kID)
        or die("Could not select the database: " . mysql_error());[/php]
        Then we'll talk.

        Ronald :cool:

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Originally posted by IsraeliShaffer
          Try changing the second parameter (username) "Robert" to "root".
          Shaffer.
          Who is Robert? And why to "root"?

          Ronald :cool:

          Comment

          • Martin Sciberras
            New Member
            • Jan 2007
            • 4

            #6
            Originally posted by ronverdonk
            Why do you post a MySQL question in the PHP forum?

            Before you give up or blame whatever, you should allow MySQL to give you a proper error message. Now you just say "it dies" and you don't even know why. If you would have included proper error handling, you would have received an error message stating the cause. So change your code into this[php]$linkID = mysql_connect(" localhost","mar tin","martin")
            or die("Could not connect to the database server: ".mysql_error() );
            mysql_select_db ("db_name",$lin kID)
            or die("Could not select the database: " . mysql_error());[/php]
            Then we'll talk.

            Ronald :cool:
            Thanks for your prompt reply.

            I've posted this question here because I'm not sure if it's something I'm doing wrong with PHP or MySQL or a combination of both.

            Even when I try the following piece of code to connect using your suggestion

            echo "Test1";
            $linkID = mysql_connect(" localhost","mar tin","martin")
            or die("Could not connect to the database server: ".mysql_error() );
            echo "Test2";

            I don't get the result of the echo statement straight after the mysql_connect. I even looked at the MySQL Administrator and I also didn't see any new connections being established. I am suspecting it may have something to do with PHP/MySQL configuration, but I haven't been able to figure it out as yet.

            Comment

            • Martin Sciberras
              New Member
              • Jan 2007
              • 4

              #7
              Originally posted by ronverdonk
              Why do you post a MySQL question in the PHP forum?

              Before you give up or blame whatever, you should allow MySQL to give you a proper error message. Now you just say "it dies" and you don't even know why. If you would have included proper error handling, you would have received an error message stating the cause. So change your code into this[php]$linkID = mysql_connect(" localhost","mar tin","martin")
              or die("Could not connect to the database server: ".mysql_error() );
              mysql_select_db ("db_name",$lin kID)
              or die("Could not select the database: " . mysql_error());[/php]
              Then we'll talk.

              Ronald :cool:
              Ronlad, thanks for your reply.

              I did the following and only the first echo statement printed:

              echo "Test1";
              $linkID = mysql_connect(" localhost","mar tin","martin")
              or die("Could not connect to the database server: ".mysql_error() );
              echo "Test2";

              Any other suggestions? Thanks.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                No, I really don't know. The only reason I can think of is that MySQL is not properly installed or configured. That is all I can think of. I hope there is someone in this forum who can shed some light on this. Sorry.

                Ronald :cool:

                Comment

                • Martin Sciberras
                  New Member
                  • Jan 2007
                  • 4

                  #9
                  Ronald, thanks for trying to help me out here.

                  I've managed to work out what the problem was. When I installed PHP, I didn't install the MySQL extenstion. Unfortunately, when I tried to connect to MySQL from my PHP script, it didn't give me any errors so that's why it was difficult to work out why it wasn't working. Now that I've installed the MySQL extention with PHP, all is working fine. Thanks again.

                  Thanks to everyone else who also made some suggestions. It was much apprecaited.

                  Regards
                  Martin

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    I am glad you posted the solution to this problem. You never know who may profit from your experience. You are always welcome in this forum.

                    Ronald :cool:

                    Comment

                    • JSW321
                      New Member
                      • Feb 2010
                      • 1

                      #11
                      And three years later I have the same problem, was about to go insane, new to php, but have worked with mySQL for 5 years.

                      Can't thank you guys enough, honest!

                      Comment

                      Working...