简体   繁体   English

尝试使用 PHP 连接到远程 SQL Server。 错误“无法建立连接”

[英]Trying to connect with PHP to remote SQL Server. Error "No connection could be made"

I am trying to to insert data from PHP form to a remote SQL Server database.我正在尝试将数据从 PHP 表单插入到远程 SQL Server 数据库。

When I clicked on a button submit get an error:当我单击按钮提交时出现错误:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\\wamp64\\www\\connection\\connection.php on line 30警告:mysqli_query() 期望参数 1 是 mysqli,资源在 C:\\wamp64\\www\\connection\\connection.php 第 30 行

And an error: Connection could not be established.并出现错误:无法建立连接。 Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 5701 [code] => 5701 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Changed database context to 'Workflow'. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Changed database context to 'Workflow'. ) [1] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 5703 [code] => 5703 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Changed language setting to us_english. [message] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Changed language setting to us_english. ) )数组 ( [0] => 数组 ( [0] => 01000 [SQLSTATE] => 01000 [1] => 5701 [代码] => 5701 [2] => [Microsoft][ODBC Driver 13 for SQL Server][ SQL Server]已将数据库上下文更改为“工作流”。[消息] => [Microsoft][ODBC 驱动程序 13 for SQL Server][SQL Server]将数据库上下文更改为“工作流”。) [1] => 数组 ( [0] => 01000 [SQLSTATE] => 01000 [1] => 5703 [code] => 5703 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]已将语言设置更改为 us_english。[消息] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]已将语言设置更改为 us_english。))

I have installed ODBC driver.我已经安装了 ODBC 驱动程序。

Connection.php:连接.php:

<?php   
$servername = "server_ip"; 
$connectionInfo=array( "Database"=>"Workflow", "UID"=>"admin", 
"PWD"=>"pass");
$conn=sqlsrv_connect($servername, $connectionInfo);

 $ID = $_POST['id'];
 $Date = $_POST['date'];


        $sql = "INSERT INTO T1 (ID,Date) VALUES ('$ID','$Date')";

 if(mysqli_query($conn,$sql)) {
    echo "Connection established.<br />";
    }else{
    echo "Connection could not be established.<br/>";
    die(print_r(sqlsrv_errors(), true));
    }


if(!mysqli_query($conn,$sql)) {
echo 'Data not inserted';
} else {
echo 'Data inserted';
}

?>

Index.html:索引.html:

<!DOCTYPE html>
<html>
    <head>    
        <link rel="stylesheet" type="text/css" href="style.css">
        <title>Form</title>
    </head>
    <body>
        <form action="connection.php" method="post">
            <div class="container">
            ID: <input type="text" name="id">
                        Date: <input type="text" name="date">
            </div>
            <input type="submit" value="insert">
        </form>
    </body>
</html>

You use functions from two different PHP extensions - sqlsrv_ and mysqli_ .您使用来自两个不同 PHP 扩展的函数 - sqlsrv_mysqli_ To connect to MS SQL Server, use sqlsrv_ functions, they are part of PHP Driver for SQL Server .要连接到 MS SQL Server,请使用sqlsrv_函数,它们是PHP Driver for SQL Server 的一部分

Also, consider the following:另外,请考虑以下事项:

  • use prepared statements:使用准备好的语句:
  • surround columns names with [] , if they are reserved keywords[]包围列名,如果它们是保留关键字

Try with this connection.php :试试这个connection.php

<?php   
# Connection
$servername = "server_ip"; 
$connectionInfo = array(
    "Database"=>"Workflow", 
    "UID"=>"admin", 
    "PWD"=>"pass"
);
$conn = sqlsrv_connect($servername, $connectionInfo);
if( $conn === false ) {
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}

# Parameters
$id = $_POST['id'];
$date = $_POST['date'];
$params = array(&$id, &$date);

# Statement
$sql = "INSERT INTO T1 (ID, [Date]) VALUES (?, ?)";
$stmt = sqlsrv_prepare($conn, $sql, $params);
if ($stmt === false) {
    echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
    exit;
}
if (sqlsrv_execute($stmt)) {
    echo "Statement executed.\n";
} else {
    echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
}

# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>

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

相关问题 如何将SQL Server连接到PHP以进行远程连接 - How to connect to sql server to PHP for remote connection 连接到SQL Server for PHP:无法建立连接 - Connect to SQL Server for PHP: Connection could not be established PHP / PDO / SQL Server。 未捕获的PDOException:找不到驱动程序 - PHP / PDO / SQL Server. Uncaught PDOException: could not find driver PHP SQL Server远程连接 - PHP SQL Server remote connection SMTP 错误:连接服务器失败:由于目标机器主动拒绝,无法连接 - SMTP ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it iODBC错误尝试使用unixODBC / FreeTDS连接到PHP中的MS SQL Server - iODBC error trying to connect to MS SQL Server in PHP with unixODBC/FreeTDS 尝试连接到远程MySQL服务器时“与MySQL服务器连接丢失” - “Lost connection to MySQL server” when trying to connect to remote MySQL server PHP ksort()函数根本无法在远程服务器上进行排序。 也不抛出错误 - PHP ksort() function is not sorting at all on remote server. Throws no error either 设置MS SQL远程连接并使用PHP从Linux主机进行连接 - Setup MS SQL Remote Connection & Connect From Linux Hosting With PHP PHP连接错误; 不允许连接到 MySQL 服务器 - PHP Connection Error; Not allowed to connect to MySQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM