PHP Fatal error: call to undefined function mssql_query ()

So I keep getting this error when I want to request something on ms sql server.

The connection is made to the database, but the queries seem to fail.

The error log contains the following:

PHP Fatal error: Call to undefined function mssql_query() 

Php code:

 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 anyone know what the problem is?

Thanks in advance!

connect.php code:

 <?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)); } ?> 
+4
source share
4 answers

You do not have MS SQL drivers. You can check this with phpinfo();

On Linux you will need mssql.so or sybase.so With debian its apt-get install php5-sybase

For windows, look here: http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx

Drivers must be configured for PHP to find the mssql _... function

You can also look at PDO DB classes, since they can connect to any DBS, you need installed drivers.

+10
source

If your connect.php code returns “Established Connection”, it means that you have correctly installed MS SQL drivers. You should use the sqlsrv_query function instead of mssql_query . The correct form for this command is:

  <?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 find out more here:

"PHP Fatal error: call to undefined function mssql_select_db () in c: \ ... appscript.php on line 16"

+2
source

you can use mssql_get_last_message () for mssql errors

0
source

As CooPer said, you should use mysql_query() instead of mssql_query() . The correct code would be:

Your code

 $sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'"; $res = mssql_query ($sql) or die(mssql_error()); 

Modified code

 $sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'"; mysql_query($sql,$con); // Here $con is your connection variable. mysql_close($con); 

Hope this helps :)

NOTE I assume that you are using mysql .

-5
source

Source: https://habr.com/ru/post/908293/


All Articles