MS SQL Server 2000 and PHP

Discussion in 'Databases' started by cre8ive, Oct 21, 2008.

  1. #1
    I have a database created inside Helm running MS SQL Server 2000. I want to connect to the database using PHP. I tried all the following codes:

    Code 1:

    <?php
    $myServer = "localhost";
    $myUser = "your_name";
    $myPass = "your_password";
    $myDB = "examples";
    
    //connection to the database
    $dbhandle = mssql_connect($myServer, $myUser, $myPass)
      or die("Couldn't connect to SQL Server on $myServer");
    
    //select a database to work with
    $selected = mssql_select_db($myDB, $dbhandle)
      or die("Couldn't open database $myDB");
    
    //declare the SQL statement that will query the database
    $query = "SELECT id, name, year ";
    $query .= "FROM cars ";
    $query .= "WHERE name='BMW'";
    
    //execute the SQL query and return records
    $result = mssql_query($query);
    
    $numRows = mssql_num_rows($result);
    echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
    
    //display the results
    while($row = mssql_fetch_array($result))
    {
      echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
    }
    //close the connection
    mssql_close($dbhandle);
    ?>
    Code (markup):
    Code 2:

    <?php
    
    //connect to a DSN "myDSN"
    $conn = odbc_connect('myDSN','','');
    
    if ($conn)
    {
      //the SQL statement that will query the database
      $query = "select * from cars";
      //perform the query
      $result=odbc_exec($conn, $query);
    
      echo "<table border=\"1\"><tr>";
    
      //print field name
      $colName = odbc_num_fields($result);
      for ($j=1; $j<= $colName; $j++)
      { 
        echo "<th>";
        echo odbc_field_name ($result, $j );
        echo "</th>";
      }
    
      //fetch tha data from the database
      while(odbc_fetch_row($result))
      {
        echo "<tr>";
        for($i=1;$i<=odbc_num_fields($result);$i++) 
        {
          echo "<td>";
          echo odbc_result($result,$i);
          echo "</td>";
        }
        echo "</tr>";
      }
    
      echo "</td> </tr>";
      echo "</table >";
    
      //close the connection
      odbc_close ($conn);
    }
    else echo "odbc not connected";
    ?>
    Code (markup):
    Code 3:

    <?php
    $myServer = "localhost";
    $myUser = "your_name";
    $myPass = "your_password";
    $myDB = "examples";
    
    //create an instance of the  ADO connection object
    $conn = new COM ("ADODB.Connection")
      or die("Cannot start ADO");
    
    //define connection string, specify database driver
    $connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;
      $conn->open($connStr); //Open the connection to the database
    
    //declare the SQL statement that will query the database
    $query = "SELECT * FROM cars";
    
    //execute the SQL statement and return records
    $rs = $conn->execute($query);
    
    $num_columns = $rs->Fields->Count();
    echo $num_columns . "<br>"; 
    
    for ($i=0; $i < $num_columns; $i++) {
        $fld[$i] = $rs->Fields($i);
    }
    
    echo "<table>";
    
    while (!$rs->EOF)  //carry on looping through while there are records
    {
        echo "<tr>";
        for ($i=0; $i < $num_columns; $i++) {
            echo "<td>" . $fld[$i]->value . "</td>";
        }
        echo "</tr>";
        $rs->MoveNext(); //move on to the next record
    }
    
    
    echo "</table>";
    
    //close the connection and recordset objects freeing up resources 
    $rs->Close();
    $conn->Close();
    
    $rs = null;
    $conn = null;
    ?>
    Code (markup):
    ( All are examples referenced on this page; http://www.webcheatsheet.com/PHP/connect_mssql_database.php )

    However, none of these codes work (on my server anyway). I received the following error messages:

    Error 1:

    Error 2:

    Error 3:

    What am I doing wrong? How can I make only one of the codes above work?
     
    cre8ive, Oct 21, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Is the site on the same server or is it on another server?
     
    jestep, Oct 21, 2008 IP
  3. cre8ive

    cre8ive Peon

    Messages:
    295
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The site is hosted on the same server as the one that hosts SQL Server.
     
    cre8ive, Oct 21, 2008 IP