4

I'm having an issue with the PDO statements for ODBC.

I'm using SQL SERVER 7 in Windows Server 2003 and PHP 5.4.x

For eg:

I have the query:

(this is not the actual query but it serves right for the example)

$query = SELECT * FROM table WHERE number = :number OR number = :number

in my php i have:

$conn = new PDO($connectionString);

$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$statement = $conn->prepare($query);

$statement->bindParam(':number', $someNumber);

$statement->execute();

This throws error

COUNT field incorrect or syntax error

The thing is, bindParam is only binding the FIRST occurrence of :number ... AND trying to bind it again doesn't work either.

Is there a way to bind multiple named params with the same name?

I'm trying not to use positional params using the ? instead

Dharman
  • 30,962
  • 25
  • 85
  • 135
Gabriel Matusevich
  • 3,835
  • 10
  • 39
  • 58
  • The only thing that I can think of to remedy this would be to do your query `$query = SELECT * FROM table WHERE number = :number1 OR number = :number2` then do `$statement->bindParam(':number1', $someNumber1); $statement->bindParam(':number2, $someNumber2);` - There are probably other ways, but that's what I came up with. Interesting problem though. – Funk Forty Niner Jun 30 '14 at 03:52
  • I have thought of that, I'm trying to avoid it, I'm making a method to search the query and replace them with ? and binding by position, although I did not want to, I think is the only solution :P – Gabriel Matusevich Jun 30 '14 at 03:54
  • If I come up with anything else, I'll let you know. Maybe someone else will have a different idea for you. – Funk Forty Niner Jun 30 '14 at 03:55
  • What is the problem with question mark parameters, if it serves your purpose quite well? – Hasib Mahmud Jun 30 '14 at 04:58
  • I just tried with question marks and it throws a different error SQLExecute[306] at ext\pdo_odbc\odbc_stmt.c:254 which is related to a bug in the odbc implementation from MS – Gabriel Matusevich Jun 30 '14 at 05:08

3 Answers3

5

Theoretical you could turn on emulation of prepared statements.

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

http://www.php.net/manual/en/pdo.prepare.php

sectus
  • 15,605
  • 5
  • 55
  • 97
1

I don't know too much about MsSQL to be honest, but I am quite sure there is some equivalent to User Defined Variables in MySQL. You could use those instead of parameters like I described in this answer:

https://stackoverflow.com/a/31068865/3391783

Community
  • 1
  • 1
low_rents
  • 4,481
  • 3
  • 27
  • 55
  • 1
    Nice. In the spirit of trying to keep information organized, if you could vote to close this question as duplicate of the other one (where your more developped answer lives), that would be great. – Félix Adriyel Gagnon-Grenier Dec 04 '19 at 19:44
0

Just turn emulation on, changing this setting from false to true:

$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345