简体   繁体   中英

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.

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

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. ) )

I have installed ODBC driver.

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>

You use functions from two different PHP extensions - sqlsrv_ and mysqli_ . To connect to MS SQL Server, use sqlsrv_ functions, they are part of 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 :

<?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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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