【问题标题】:Trying to connect with PHP to remote SQL Server. Error "No connection could be made"尝试使用 PHP 连接到远程 SQL Server。错误“无法建立连接”
【发布时间】:2019-04-16 06:39:11
【问题描述】:

我正在尝试将数据从 PHP 表单插入到远程 SQL Server 数据库。

当我点击一个按钮提交得到一个错误:

警告:mysqli_query() 期望参数 1 为 mysqli,第 30 行 C:\wamp64\www\connection\connection.php 中给出的资源

还有一个错误:无法建立连接。数组( [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 [代码] => 5703 [2] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]将语言设置更改为 us_english。 [留言] => [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]更改语言 设置为 us_english。 ) )

我已经安装了 ODBC 驱动程序。

Connection.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';
}

?>

索引.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>

【问题讨论】:

  • 你为什么要使用 mysql 接口连接到 SQL-Server。你有哪一个?另请阅读How can I prevent SQL injection in PHP?
  • @danblack 我的数据库在远程机器上的 SQL Server 上。我想制作一个 php 表单,将数据发送到 SQL Server 中的表。我已经在本地电脑上设置了 WAMP,以使用 PHP 表单托管我的网站。我应该选择不同的方法吗?

标签: php html mysql sql-server sqlsrv


【解决方案1】:

您使用来自两个不同 PHP 扩展的函数 - sqlsrv_mysqli_。要连接到 MS SQL Server,请使用 sqlsrv_ 函数,它们是 PHP Driver for SQL Server 的一部分。

另外,请考虑以下事项:

  • 使用准备好的语句:
  • 如果是保留关键字,则使用 [] 将列名括起来

试试这个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);
?>

【讨论】:

    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2019-08-09
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多