gpt4 book ai didi

php - 使 PHP 代码与 MySQL 一起使用 MsSQL

转载 作者:行者123 更新时间:2023-11-29 10:44:06 25 4
gpt4 key购买 nike

我们有一个由一组学生开发的项目,作为他们硕士学位期末项目的一部分。整个解决方案效果很好,不幸的是它是为 MySQL 数据库开发的,而我们使用的是 MsSQL Server 数据库。

我一直在努力等待让它发挥作用,但我并没有真正取得任何进展。在本地计算机上,我正在运行带有 PHP 5.3.29 和 Sql Server 2012 的 Apache 服务器。

来自 phpinfo() 的信息;Apache 版本 Apache/2.2.25 (Win32) PHP/5.3.29我在 phpinfo() 中看不到 mssql,但在 PHP.ini 中我确实有以下内容:

[MSSQL]
; Allow or prevent persistent links.
mssql.allow_persistent = On

; Maximum number of persistent links. -1 means no limit.
mssql.max_persistent = -1

; Maximum number of links (persistent+non persistent). -1 means no limit.
mssql.max_links = -1

; Minimum error severity to display.
mssql.min_error_severity = 10

; Minimum message severity to display.
mssql.min_message_severity = 10

; Compatibility mode with old versions of PHP 3.0.
mssql.compatability_mode = Off

; Connect timeout
;mssql.connect_timeout = 5

; Query timeout
;mssql.timeout = 60

; Valid range 0 - 2147483647. Default = 4096.
;mssql.textlimit = 4096

; Valid range 0 - 2147483647. Default = 4096.
;mssql.textsize = 4096

; Limits the number of records in each batch. 0 = all records in one batch.
;mssql.batchsize = 0

; Specify how datetime and datetim4 columns are returned
; On => Returns data converted to SQL server settings
; Off => Returns values as YYYY-MM-DD hh:mm:ss
;mssql.datetimeconvert = On

; Use NT authentication when connecting to the server
mssql.secure_connection = On

; Specify max number of processes. -1 = library default
; msdlib defaults to 25
; FreeTDS defaults to 4096
;mssql.max_procs = -1

; Specify client character set.
; If empty or not set the client charset from freetds.conf is used
; This is only used when compiled with FreeTDS
;mssql.charset = "ISO-8859-1"

我尝试过以下方法:dbconnect.php

$myServer = "localhost";
$myUser = "sa";
$myPass = "sa123";
$myDB = "st";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

die();
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");

我首先尝试让系统允许我登录,所以login.php代码如下。我已将查询更改为 mssql:

    <?php
error_reporting(E_ALL);
session_start(); // Starting Session
require("includes/db_connect.php");
$hint = "";
$username=$_POST["username"];
$password=$_POST["password"];
/*=============================================================
SQL INJECTION PREVENTION
===============================================================*/
$PRElist = array();
$PREsql = "SELECT Username, Password FROM tblUsers ;";
$PREresult = mssql_query($PREsql);
//if (mysqli_num_rows($PREresult)>0)
if (1 == 1){
// output data of each row
while($row = mssql_fetch_assoc($PREresult)) {
$PRElist[]= strtolower($row['Username']);
$PRElist[strtolower($row['Username'])]=$row['Password'];
}
}//to prevent sql injection
//=======================START LOOKING UP THE USER==================
if ((in_array(strtolower($username), $PRElist))&&($PRElist[strtolower($username)]==$password))
{
$sql = "SELECT UserId, Username, Password FROM tblUsers where Username='$username' AND Password='$password'";
$result = mssql_query($sql);
$numRows = mssql_num_rows($result);
if ($numRows > 0) {
// output data of each row
while($row = mssql_fetch_assoc($result)) {
$hint=""; //initialize the hint string..
if (strtolower($username)==strtolower($row["Username"])){
$userID= $row["UserId"];
$sql = "SELECT GroupId FROM tblUserGroups where UserId='$userID'";
$result = mssql_query($sql);
$numRows1 = mssql_num_rows($result);
if ($numRows1 > 0) {
// output data of each row
while($row = mssql_fetch_assoc($result)) {
switch ($row["GroupId"]) {
case '1':
header("location: home.php"); // Redirecting To Other Page
$hint="<span style='color:green'> This username is registered </span>";
$_SESSION['login_user']=$username; // Initializing Session
$_SESSION['login_pass']=$password; // Initializing Session# code...
$_SESSION['userID']=$userID; // Initializing Session# code...
break;
case '2':
header("location: Team_Home.php"); // Redirecting To Other Page
$hint="<span style='color:green'> This username is registered </span>";
$_SESSION['login_user']=$username; // Initializing Session
$_SESSION['login_pass']=$password; // Initializing Session# code...
$_SESSION['userID']=$userID; // Initializing Session# code...
break;
case '3':
header("location: Staff_Home.php"); // Redirecting To Other Page
$hint="<span style='color:green'> This username is registered </span>";
$_SESSION['login_user']=$username; // Initializing Session
$_SESSION['login_pass']=$password; // Initializing Session# code...
$_SESSION['userID']=$userID; // Initializing Session# code...
break;
default:
$hint="<span style='color:red'>Not registered...</span>";
header("location: index.php"); // Redirecting To Other Page
break;
}
}
}


}
else
{
$hint="<span style='color:red'>Not registered...</span>";
header("location: index.php"); // Redirecting To Other Page

}
}
}
}
else{
header("location: index.php"); // Redirecting To Other Page
$hint="<span style='color:red'>Not registered...</span>";
}
echo $hint;
mssql_close($conn);

我无法真正看到发生了什么,因为当我尝试登录时,我只是看到一个白屏,控制台中没有任何信息或错误。

最佳答案

$myServer = "localhost";
$myUser = "sa";
$myPass = "sa123";
$myDB = "st";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

die(); // whats this for? it can cause white screen.**



$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");

关于php - 使 PHP 代码与 MySQL 一起使用 MsSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44950874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com