简体   繁体   English

将PHP与MS SQL SERVER连接

[英]Connect PHP with MS SQL SERVER

I am new to PHP and I am trying to connect my PHP with MS SQL SERVER. 我是PHP的新手,正在尝试将PHP与MS SQL SERVER连接。 I have googled it but not found any good solution. 我用谷歌搜索,但没有找到任何好的解决方案。

I am using PHP version : 7.0.6 我正在使用PHP版本:7.0.6

I have downloaded the required extension and place it in xampp/php/ext folder and added these lines in php.ini file 我已经下载了所需的扩展名并将其放在xampp / php / ext文件夹中,并将这些行添加到php.ini文件中

extension=php_pdo_sqlsrv_7_nts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_nts_x64.dll

and I m using this code to connect to my server. 我正在使用此代码连接到我的服务器。

$myServer = "SERVER_IP"; 
$myUser = "USER_NAME"; 
$myPass = "PASSWORD"; 
$myDB = "DB_NAME"; 

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

bu tit shows me this error: bu tit向我显示此错误:

Fatal error: Uncaught Error: Call to undefined function mssql_connect() in C:\xampp\htdocs\schedule\server.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\schedule\server.php on line 2

Any help in this would be highly appreciated !! 任何帮助,将不胜感激!

You have sqlsrv_connect not mssql_connect, try using this. 您有sqlsrv_connect而不是mssql_connect,请尝试使用此功能。 If it doesn't work, that means you have problems with your extension ( you can also use function_exists to check ). 如果它不起作用,则意味着您的扩展程序有问题(也可以使用function_exists进行检查)。

More info: sqlsrv_connect: http://php.net/manual/ro/function.sqlsrv-connect.php 更多信息:sqlsrv_connect: http ://php.net/manual/ro/function.sqlsrv-connect.php

$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database_name", "UID"=>"mssql_username", "PWD"=>"mssql_password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Error connecting";
     die( print_r( sqlsrv_errors(), true));
}

I believe you have to add that mssql extension in order to use that... but you can Use PDO if you want to.. follow the link here 我相信您必须添加该mssql扩展才能使用它...但是如果您愿意,可以使用PDO。

http://php.net/manual/en/ref.pdo-dblib.php http://php.net/manual/zh/ref.pdo-dblib.php

Issue Solved: 解决的问题:

I have used extension=php_odbc.dll 我用过extension = php_odbc.dll

with this code: 使用此代码:

$server = '****';
    $user = '****';
    $pass = '****';
    //Define Port
    $port='Port=1433';
    $database = 'cargo_web';

    $connection_string = "DRIVER={SQL Server};SERVER=$server;$port;DATABASE=$database";
    $conn = odbc_connect($connection_string,$user,$pass);
    if ($conn) {
        echo "Connection established.";
    } else{
        die("Connection could not be established.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM