I need to know how to handle <> operator used with a parameter in where clause within a prepared statement. Can anyone please show me how to do something similar to 'WHERE field <>:parameter'. Thanks. Edit : I did not remember to mention that MySQL is my database server.
Asked
Active
Viewed 286 times
1 Answers
0
Up until I discovered this, In ALL examples on the web, I had only seen '=' operator in WHERE clause of prepared statements. But I recently found out that ANY, and I mean ANY, operator logical operator works with prepared statement placeholders, not just '='. As an example, you could have:
<?php
$stmt = $dbHandle->prepare( 'SELECT * FROM table WHERE id <> :ID' );
$stmt->execute( array( ':ID'=> $id ) );
?>
OR even this
<?php
$stmt = $dbHandle->prepare( 'SELECT * FROM table WHERE id <= :ID' );
$stmt->execute( array( ':ID'=> $id ) );
?>
AND, as you may guess,
<?php
$stmt = $dbHandle->prepare( 'SELECT * FROM table WHERE id >= :ID' );
$stmt->execute( array( ':ID'=> $id ) );
?>
It looks logical that PDO should be able to do this but was not sure. So, it was a relief to me to discover this through experimentation. I had always wanted to do that in my code. I hope this saves someone some time.

Stephen Adelakun
- 784
- 2
- 7
- 24