SQL server php登录脚本


SQL server php login script

我已经创建了脚本来验证我的登录系统,但我很难弄清楚如何使其专门登录到我的sqlserver帐户到我的特定工作空间,我的登录脚本到目前为止如下

<?php
    session_start();
    if(!isset($_SESSION["user_id"])){
        header("location:../../login.html");
    }
    $username = $_POST['txt_username'];
    $user_id = $_POST['txt_password'];

    mysql_connect($server, $username, $password) or die("No Server Found");
    mysql_select_db($schema) or die("No Connection");
?>

阅读:http://php.net/manual/en/function.mssql-connect.php

下面是连接到MSSQL Server数据库的代码

//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);
?>

连接DSN

ODBC函数

DSN表示'数据源名称'。这是一种为数据源分配有用且容易记住的名称的简单方法,这些数据源可能不仅仅局限于数据库。

在下面的示例中,我们将向您展示如何将DSN连接到名为'examples'的MSSQL Server数据库,并从'cars'表中检索所有记录。

<?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"; 
?>