正在尝试通过 PHP 连接到远程 SQL 服务器。错误 "No connection could be made"
Trying to connect with PHP to remote SQL Server. Error "No connection could be made"
我正在尝试将数据从 PHP 表单插入远程 SQL 服务器数据库。
当我点击提交按钮时出现错误:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\wamp64\www\connection\connection.php on line 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. ) )
我已经安装了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';
}
?>
Index.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>
您使用来自两个不同 PHP 扩展的函数 - sqlsrv_
和 mysqli_
。要连接到 MS SQL 服务器,请使用 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);
?>
我正在尝试将数据从 PHP 表单插入远程 SQL 服务器数据库。
当我点击提交按钮时出现错误:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\wamp64\www\connection\connection.php on line 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. ) )
我已经安装了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';
}
?>
Index.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>
您使用来自两个不同 PHP 扩展的函数 - sqlsrv_
和 mysqli_
。要连接到 MS SQL 服务器,请使用 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);
?>