[英]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:另外,请考虑以下事项:
[]
, 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.