php 警告:sqlsrv_query() 期望参数 1 是资源,给定的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23803275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:57:31  来源:igfitidea点击:

Warning: sqlsrv_query() expects parameter 1 to be resource, string given

phpsql-serverdatabaseiis

提问by klapsius

Setup

设置

MS IIS server

微软IIS服务器

MSSQL DB Server

MSSQL 数据库服务器

PHP

PHP

Error

错误

Warning: sqlsrv_query() expects parameter 1 to be resource, string given in C:\inetpub\wwwroot\creating_new_table.php on line 36
Table creation failed with error:\n
Fatal error: Call to undefined function sqlsrv_get_last_message() in C:\inetpub\wwwroot\creating_new_table.php on line 39

Code

代码

$serverName ="NAME\SQLEXPRESS";
$usr="sa";
$pwd="pasw";
$db="DBNAME";

$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);

$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn )
{
    echo "Connected";
}
else
{
    echo "Error";
    die( print_r( sqlsrv_errors(), true));
}

$sql = "CREATE TABLE fyi_links ("
    . " id INT NOT NULL VARCHAR (6)" 
    . ", url VARCHAR(80) NOT NULL"
    . ", notes VARCHAR(1024)"
    . ", counts INT"
    . ", time DATETIME"
    . ")";
$res = sqlsrv_query($sql,$conn);
if (!$res) {
    print('Table creation failed with error:\n');
    print("   ".sqlsrv_get_last_message()."\n");
}
else {
    print("Table fyi_links created.\n");
}  

mssql_close( $conn);

Connection is fine but something happens with my create table script.

连接很好,但我的创建表脚本发生了一些事情。

回答by Ian

$res = sqlsrv_query($sql,$conn);should be $res = sqlsrv_query($conn, $sql);

$res = sqlsrv_query($sql,$conn);应该 $res = sqlsrv_query($conn, $sql);

See the manual http://us3.php.net/manual/en/function.sqlsrv-query.php

参见手册http://us3.php.net/manual/en/function.sqlsrv-query.php