简体   繁体   中英

PHP PDO dblib parameters not bound

I have to connect to an external SQL database. I am using the PDO dblib

The connection to the database works fine. Once I try to use the prepare statement, the parameters are not set.

$dbh = new PDO(sprintf('dblib:host=%s;dbname=%s',self::DB_HOST,self::DB_NAME), self::DB_USERNAME, self::DB_PASSWORD);

$dbh->prepare("SELECT * FROM Contacts WHERE ref = :id");
$sth->execute(array(':id' => 1172));
$result =  $sth->fetchAll(PDO::FETCH_CLASS);

returns an empty array

Another attempt according to the documentation: ( http://php.net/manual/en/pdostatement.execute.php )

$dbh->prepare("SELECT * FROM Contacts WHERE ref = ?");
$sth->execute(array(1172));
$result =  $sth->fetchAll(PDO::FETCH_CLASS);

also returns an empty array

Third attempt without parameters:

$dbh->prepare("SELECT * FROM Contacts WHERE ref = 1172");
$sth->execute();
$result =  $sth->fetchAll(PDO::FETCH_CLASS);

does return a result

I also tried the $sth->bindParam() and $sth->bindValue() but I do not get any result.

I installed the dblib on a linux server , connecting to an external Microsoft 2008 SQL server.

Am I doing it in a wrong way ?

From the documentation of PDOStatement::execute :

input_parameters

An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR .

The last sentence could be the source of your problem: maybe in your database 1172 is not equal to '1172' . Check from the CLI if the result of the following query is 0 :

SELECT 1172 = '1172';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM