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

时间:2014-05-22 09:48:29

标签: php sql-server database iis

设置

MS IIS服务器

MSSQL数据库服务器

PHP

错误

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

代码

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

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

1 个答案:

答案 0 :(得分:3)

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

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