Executing SQL directly; no cursor., SQL state 0100

2019-06-06 18:14发布

问题:

Im using WAMP sever PHP 5.3.4 and sql server 2008 r2 connect using the driver odbc driver 2011 When i execute this query i got the correct answer,

SELECT [password],[username] FROM [customer]

But having an error While execute the below query,i checked the same query in the sqlserver it gave me the result corrcetly

SELECT [password],[username] FROM customer Where $name=[password] and $pass=[username]

This is what the error im getting

A PHP Error was encountered

Severity: Warning

Message: odbc_exec(): SQL error: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Executing SQL directly; no cursor., SQL state 01000 in SQLExecDirect

Filename: models/general_account.php

Line Number: 18

UPDATE:

function login($name,$pass)
{
var_dump($name);
var_dump($pass);        
$this->load->database();
$conn=odbc_connect('odbcconnection','sa','sa') or die ('error');
$sql='SELECT [password],[username] FROM customer Where $name=[password] and $pass=[username]';          
$rs=odbc_exec($conn,$sql) or die('exe error');            
odbc_close($conn); 
}    

Thanks in Advance.

回答1:

Its a silly mistake made by myself,

$sql='SELECT [password],[username] FROM customer Where $name=[password] and $pass=[username]';  

i just print the sql query stored in $sql variable and run that query on the MSSQL Server, the query comes likes this,

SELECT [password],[username] FROM customer Where asd=[password] and asd123=[username]

but in sql server we need to give the string in the quotes

$sql="SELECT [password],[username] FROM customer Where '$name'=[password] and '$pass'=[username]";

And it woks fine...



回答2:

I had same error and it was because I had installed the new SQL Server Version 11 odbc driver that was needed for SQl Server 2014 database but there was another database that was on SQL Server 2008 r2 that still needed the older SQL Server driver.