无法使用sqlsrv_connect()连接到数据库

时间:2017-04-10 06:57:34

标签: php database-connection sql-insert sqlsrv

我希望使用php存储html输入数据存储sql server数据库,只是可能知道给你的解决方案尽可能忽略    这是代码......

    <?php
    $serverName = 'SRB-Nick_Desktop\SQLEXPRESS';
    $connectionInfo = array('Database'=>'cslogs', 'UID'=>'cslogslogin', 
   'PWD'=>'123456');
    $connection = sqlsrv_connect($serverName, $connectionInfo);
   if( $connection === false )
   {
    echo "Connection could not be established.\n";
   die( print_r( sqlsrv_errors(), true));
    }
   $tsql = "INSERT INTO logs(ForteID, disposition, appnumber, Finance_Num, 
   num_payments,   ach_cc, date, notes) VALUES (?,?,?,?,?,?,?,?)";
   $parameters = array( "forteid", "LOC", "NCXXXXXXX4", "SRB-000004", "0", 
   "cc", "2012-11-01", "gave LOC instructions");
  $stmt = sqlsrv_query($connection, $tsql, $parameters);
   if( $stmt === false ){
  echo "Statement could not be executed.\n";
  die( print_r( sqlsrv_errors(), true));
  } 
else
{
echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."\n";
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $connection);
?>

1 个答案:

答案 0 :(得分:-2)

真的不清楚你在问什么,但这是正常的程序。

    <?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = sqlsrv_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . sqlsrv_errors());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (sqlsrv_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . sqlsrv_errors($conn);
}

sqlsrv_close($conn);
?>

查看php文档以获取进一步的参考。 http://php.net/manual/en/book.sqlsrv.php