2

Setup

MS IIS server

MSSQL DB Server

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.

Re Captcha
  • 3,125
  • 2
  • 22
  • 34
klapsius
  • 75
  • 2
  • 2
  • 8
  • 4
    `" id INT NOT NULL VARCHAR (6)" ` WTF?!? – Mark Baker May 22 '14 at 09:50
  • Im just beginer for mssql. Sorry for that – klapsius May 22 '14 at 09:51
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – HamZa May 22 '14 at 09:51
  • 1
    The only way a column can have two different datatypes such as varchar and int at the same time is if you're using the Schrödinger database engine, and even then the datatype is only both until you actually observer it, whereupon it resolves into one or the other datatype – Mark Baker May 22 '14 at 09:53
  • Another error:Fatal error: Call to undefined function sqlsrv_get_last_message() in – klapsius May 22 '14 at 09:59
  • Perhaps you're thinking of [sqlsrv_errors()](http://us3.php.net/manual/en/function.sqlsrv-errors.php) rather than `sqlsrv_get_last_message()` – Mark Baker May 22 '14 at 10:00

1 Answers1

3

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

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

Ian
  • 3,539
  • 4
  • 27
  • 48
  • @klapsius Good to hear, please mark my answer as correct, cheers – Ian May 22 '14 at 09:55
  • Another error:Fatal error: Call to undefined function sqlsrv_get_last_message() in – klapsius May 22 '14 at 09:56
  • Perhaps you're thinking of [sqlsrv_errors()](http://us3.php.net/manual/en/function.sqlsrv-errors.php) rather than `sqlsrv_get_last_message()` – Mark Baker May 22 '14 at 09:58