PHP 致命错误:调用未定义的函数 mssql_query()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17103751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: Call to undefined function mssql_query()
提问by M.B.
So I keep getting this error when I want to query something to the ms sql server..
因此,当我想向 ms sql 服务器查询某些内容时,我不断收到此错误..
The connection is made with the database but the queries seem to fail.
已与数据库建立连接,但查询似乎失败。
The error log contains this:
错误日志包含以下内容:
PHP Fatal error: Call to undefined function mssql_query()
The code on the php:
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(); } } ?>
Does anybody knows what the problem is?
有谁知道问题是什么?
Thanks in advance!
提前致谢!
connect.php code:
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));
}
?>
回答by Daniel W.
You don't have the MS SQL Drivers installed.
You can check this with phpinfo();
您没有安装 MS SQL 驱动程序。你可以检查这个phpinfo();
On Linuxyou need mssql.so or sybase.so
With debian its apt-get install php5-sybase
在Linux 上你需要 mssql.so 或 sybase.so 在 debian 它apt-get install php5-sybase
For windowstake a look here: http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
对于Windows,请查看此处:http: //msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
Drivers need to be configured for PHP to find the function mssql_...
需要为 PHP 配置驱动程序才能找到函数 mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
您还可以查看 PDO DB 类,因为它们可以连接到任何 DBS,您需要安装驱动程序。
回答by Alireza
If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly.
You have to use sqlsrv_query
function instead of mssql_query
.
The correct form of this command is:
如果您的 connect.php 代码返回“已建立连接。”,则表示您正确安装了 MS SQL 驱动程序。您必须使用sqlsrv_query
function 而不是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));
}
?>
you can learn more here:
您可以在此处了解更多信息:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
“PHP 致命错误:在第 16 行调用 c:\...appscript.php 中未定义的函数 mssql_select_db()”
回答by Alireza
you can use mssql_get_last_message()for mssql errors
您可以将mssql_get_last_message()用于 mssql 错误