balaji balaji - 3 months ago 29x
PHP Question

Executing SQL directly; no cursor., SQL state 01000 in SQLExecDirect

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


function login($name,$pass)
$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');

Thanks in Advance.


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...