Connecting PHP to SQL Server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hamayun Khan
    New Member
    • Aug 2007
    • 106

    #1

    Connecting PHP to SQL Server

    Hi

    I want to connect PHP to SQL Server. I use the following code [php]<?
    $connection = mssql_connect($ host,$username, $password) or die("Couldn't connect to SQL Server on");
    ?>[/php]but nothing is displayed. I don't know what are the requirements for connecting to sql server

    looking for ur help.
    Thank's
    Last edited by ronverdonk; Apr 17 '08, 05:43 PM. Reason: code tags
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Greetings and welcome.

    Nothing is displayed because you have not asked it to display anything. If all the information you have provided it is correct ($host, $username, $password) it should connect. How to test if it has is adding one line of code:
    (I havce changed this to mysql instead of your mssql, so provided the syntax is the same, just change that (I have never used mssql)
    [PHP]<?php
    $connection = mysql_connect($ host,$username, $password) or die('I cannot connect to MySQL because: ' . mysql_error());
    echo('I have successfully connected');
    ?>[/PHP]

    What I have done:
    1. Don't be lazy - Write out <?php instead of just <? for good practice and in future versions of php that will not work I believe.
    2. In the die() function I have written your message (I cannot connect to MySQL because:) in a string and then combined it with the mysql_error() function which displays the error that it has run into. Mssql might have a different function, I am not sure.
    3. Made an echo statement which will mean something is displayed on the screen if it ran successfully. The die() function will stop the code there if there is a problem and not display the echo line, but only the die() error.

    Hope that helps, but I recommend looking at some tutes if you are still confused.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by Hamayun Khan
      Hi

      I want to connect PHP to SQL Server. I use the following code
      <?
      $connection = mssql_connect($ host,$username, $password) or die("Couldn't connect to SQL Server on");
      ?>

      but nothing is displayed. I don't know what are the requirements for connecting to sql server

      looking for ur help.
      Thank's
      The very fact that nothing is written to the browser shows you have successfully connected to your database.

      Congratulations :)

      Comment

      • Hamayun Khan
        New Member
        • Aug 2007
        • 106

        #4
        Thanks for replying. Sorry for writing incomplete code. I use[php]<?
        error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
        $host="LOCALHOS T";
        $database="test generator";
        //$connection = mysql_connect($ host,$username, $password);
        $connection = mssql_connect('HAMAYUN\\SQLSe rver','sa','hama') or die("SQL Server Error");
        echo "hello";
        ?>[/php]
        This display nothing the time when i post on the form.
        By searching the google I find that I have to uncomment the line

        extension=php_m ssql.dll in php.ini.
        Now die("SQL Server Error"); is executed and i see the following output on page
        SQL Server Error. It means I cannot connect to sql server.
        Also I used the following[php]<?
        $connection = mssql_connect('HAMAYUN\SQLSer ver','sa','hama') or die("SQL Server Error");
        $connection = mssql_connect('Localhost\SQLS erver','sa','hama') or die("SQL Server Error");
        $connection = mssql_connect('Localhost','sa','hama') or die("SQL Server Error");
        $connection = mssql_connect('.\SQLServer','sa','hama') or die("SQL Server Error");
        $connection = mssql_connect('My IP','sa','hama') or die("SQL Server Error");
        ?>[/php]
        as mension in some helps but no succes.
        Sql server management studio use the following to connect to sql server
        "Hamayun\SQLSer ver","sa","hama "
        Now how to connect to sql server 2005.
        Looking for quick Help
        Thanks
        Last edited by ronverdonk; Apr 16 '08, 06:11 PM. Reason: warning: use code tags!!

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by Hamayun Khan
          Thanks for replying.

          Sorry for writing incomplete code. I use

          <?
          error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
          $host="LOCALHOS T";
          $database="test generator";
          //$connection = mysql_connect($ host,$username, $password);
          $connection = mssql_connect('HAMAYUN\\SQLSe rver','sa','hama') or die("SQL Server Error");

          echo "hello";
          ?>

          This display nothing the time when i post on the form.
          By searching the google I find that I have to uncomment the line

          extension=php_m ssql.dll in php.ini.
          Now die("SQL Server Error"); is executed and i see the following output on page

          SQL Server Error. It means I cannot connect to sql server.

          Also I used the following

          <?
          $connection = mssql_connect('HAMAYUN\SQLSer ver','sa','hama') or die("SQL Server Error");
          $connection = mssql_connect('Localhost\SQLS erver','sa','hama') or die("SQL Server Error");
          $connection = mssql_connect('Localhost','sa','hama') or die("SQL Server Error");
          $connection = mssql_connect('.\SQLServer','sa','hama') or die("SQL Server Error");
          $connection = mssql_connect('My IP','sa','hama') or die("SQL Server Error");
          ?>
          as mension in some helps but no succes.
          Sql server management studio use the following to connect to sql server
          "Hamayun\SQLSer ver","sa","hama "

          Now how to connect to sql server 2005.

          Looking for quick Help
          Thanks
          You're not using any statements to check the connection:
          [php]
          $connection = mssql_connect(C ONNECTION_DETAI LS) or die(mssql_error );
          if($connection)
          {
          echo "Connected" ;
          }
          else
          {
          echo "Problem with connection";
          }
          [/php]
          Notice the mssql_error - using this will give you exact error info.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            warning:

            Please enclose your posted code in [code] tags (See How to Ask a Question).

            This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

            Please use [code] tags in future.

            MODERATOR

            Comment

            • Hamayun Khan
              New Member
              • Aug 2007
              • 106

              #7
              I get the out put "SQL Server Error" which is the die() statement of the line 6 of code[php]<?
              error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
              $host="LOCALHOS T";
              $database="test generator";
              //$connection = mysql_connect($ host,$username, $password);
              $connection = mssql_connect('HAMAYUN\\SQLSe rver','sa','hama') or die("SQL Server Error");
              echo "hello";
              ?>[/php]this means that there is some problem while connecting the server.
              Last edited by ronverdonk; Apr 17 '08, 05:43 PM. Reason: 2nd warning: use code tags!

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Hamayun Khan
                I get the out put "SQL Server Error" which is the die() statement of the line 6 of code

                <?
                error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
                $host="LOCALHOS T";
                $database="test generator";
                //$connection = mysql_connect($ host,$username, $password);
                $connection = mssql_connect('HAMAYUN\\SQLSe rver','sa','hama') or die("SQL Server Error");
                echo "hello";
                ?>

                this means that there is some problem while connecting the server.
                Right.
                Take a look at my last post and change the part that says
                [php]
                mssql_error()
                [/php]
                Change it to
                [php]
                mssql_get_last_ message()
                [/php]

                Regards.

                Comment

                • Hamayun Khan
                  New Member
                  • Aug 2007
                  • 106

                  #9
                  I use this [php]<?
                  mssql_connect(" Hamayun\SQLServ er","xxx","xxx" ) or die("SQL Error:" . mssql_get_last_ message());
                  ?>[/php]
                  the only output i get
                  SQL Error:
                  Last edited by ronverdonk; Apr 17 '08, 05:41 PM. Reason: code tags

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

                    moderator

                    Comment

                    • Hamayun Khan
                      New Member
                      • Aug 2007
                      • 106

                      #11
                      Originally posted by ronverdonk
                      Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

                      moderator

                      Sorry for not using the right code tags.

                      I m using <? ?> for php <% %> for asp. I don't know what r the right code tags.

                      Would you please give me one example so that i will use in future.

                      Thanks

                      Comment

                      • Hamayun Khan
                        New Member
                        • Aug 2007
                        • 106

                        #12
                        Originally posted by ronverdonk
                        Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

                        moderator
                        Is this is right.


                        [php]<?
                        error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
                        $host="LOCALHOS T";
                        $database="test generator";
                        //$connection = mysql_connect($ host,$username, $password);
                        $connection = mssql_connect('HAMAYUN\\SQLSe rver','sa','hama') or die("SQL Server Error");
                        echo "hello";
                        ?>[/php]

                        Comment

                        • Hamayun Khan
                          New Member
                          • Aug 2007
                          • 106

                          #13
                          Originally posted by ronverdonk
                          Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

                          moderator

                          [php]
                          <?
                          mssql_connect(" Hamayun\\SQLSer ver","xxx","xxx ") or die("Error")
                          ?>[/php]


                          OK I find the way
                          Thank's

                          Comment

                          Working...