因此,当我想向ms sql server查询内容时,我一直收到此错误。
与数据库建立连接,但查询似乎失败。
错误日志包含:
PHP Fatal error: Call to undefined function mssql_query()
php上的代码:
session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());
if (mssql_num_rows($res) == 1) {
$row = mssql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];
header("Location: test.php");
exit();
} else {
echo "Invalid login information. Please return to the previous page.";
exit(); } } ?>
有人知道问题是什么吗?
提前致谢!
connect.php代码:
<?php
$serverName = "MTN-TEST"; //serverName\instanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "<span style='color:green;'>Connection established.</span><br />";
}else{
echo "<span style='color:red;'>Connection could not be established.</span><br />";
die( print_r( sqlsrv_errors(), true));
}
?>
答案 0 :(得分:10)
您没有安装MS SQL驱动程序。
您可以使用phpinfo();
在 Linux 上,您需要mssql.so或sybase.so
使用debian apt-get install php5-sybase
对于 windows ,请看一下: http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
需要为PHP配置驱动程序才能找到函数mssql _...
您还可以查看PDO数据库类,因为它们可以连接到任何DBS,您需要安装驱动程序。
答案 1 :(得分:3)
如果您的connect.php代码返回“已建立连接。”,则表示您正确安装了MS SQL驱动程序。
您必须使用sqlsrv_query
函数而不是mssql_query
。
该命令的正确形式是:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query='select * from test';
$result = sqlsrv_query($conn,$query);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
你可以在这里了解更多:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
答案 2 :(得分:0)
您可以将mssql_get_last_message()用于mssql错误