Jump to content

Binding null to a parameter


txrandom

Recommended Posts

$departmentRow = $departmentTable->fetchRow(array("department = ?" => $department));

 

Let's say there is an off chance $department may be null.  Doing this with Zend Framework throws a 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound'  Is there any way to allow $department to be null?

Link to comment
Share on other sites

What would the point of that be?  If the $department has no value you will not find any rows, so there is no point in doing the query.  You should simply check for that and bypass the query altogether. 

 

If you really want to search for NULL (which should be avoided in my opinion)  then the clause required is WHERE column IS NULL, so at minimum the string passed would probably need to be 'department IS NULL' rather than your array with bind variable.

Link to comment
Share on other sites

That was a bad example:

 

$slotRow = $slotTable->fetchRow(array("locationId = ?" => $locationRow->locationId, "timeId = ?" => $timeRow->timeId, "days = ?" => $days));

 

I'm using this for a data scrubber.  I don't want to replicate data, so I need to make sure it's not already in there.  For example, a slot may have the time and day fields filled, but this slot may not have a location.  The location should be null since it doesn't have one.  Well what if there is another slot with the same time and day, but actually has a location?  I need to make sure I check whether that locationId is null.

 

Does that make sense?  Do you think there is a better approach to this?

Link to comment
Share on other sites

Well I think I found a work around.  I'm already detecting to see if the valid is null.  I'm just going to change the "locationId = ?" to a variable and have the other option be "locationId IS NULL"  That way it doesn't even matter if the parameter is null since it won't be binding it anyways.  Thanks for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.