I had a problem binding a null parameter in the following code
$nullVariable = NULL; $sql = new PDO('mysql:host=' . $Server, $User, $Password); $sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $statement = $sql->prepare("SELECT * FROM Table WHERE Binary16Column = :uuid"); $statement->bindParam(":uuid", $nullVariable, PDO::PARAM_NULL); $statement->execute(); $results = $statement->fetchAll(PDO::FETCH_ASSOC);
The result variable will be an empty array. If I do not use the parameters and change my query to "WHERE Binary16Column IS NULL", it returns the expected number of rows. Therefore, the problem should be how I process the parameter, and not in my SQL query.
My code is more complex than the one listed above, and I need to be able to use a parameter variable, which can be null, so checking for a null variable and executing another query are less ideal. Technically, I have my own function for setting parameters, this is where I check to see if the contents of the variable are null and bind this parameter accordingly, so I don't need to write an unnecessary number of queries. Query execution also works fine if the variable contains valid data and the parameter type is PARAM_LOB.
Does anyone know what I'm doing wrong? Many thanks!
Matt
source share