Search code examples
phppdoodbcmaxdb

PHP PDO Params - Empty strings in $data array cause query to fail


I have a PDO issue with PHP 5.3. I am running MaxDB 7.8 for my database - if you haven't heard of that, you're not alone. It's an open-source enterprise database that is connected with MySQL on paper, but is nothing like it.

I'm not sure that this issue is caused by MaxDB, but I wanted to mention it.

I found that when using prepared statements via the ODBC driver, my query fails if ANY of the $data array values are empty strings. For example, a user is presented with a dialog box where 1 value is required ('title'), but 2 are optional ('author', 'version'). If the user chooses not to enter one of the optional values, that array element would be == "". If this is the case, the $sth->execute($data) fails, complaining of null errors.

I have tried the setAttribute command re: Nulls on all 3 settings, I have tried checking if $value == null { $value = ""; }... and numerous other things I've found in articles, all to no avail. If I manually replace any empty values with a string like "(none)", the query works.

Anyways, here's the particulars:

$data:

Array ( [cust] => 1 [ftype] => 1 [title] => test [author] => [version] => [folder] => 0 [modified] => 1337394898 [content] => "this is test content" [status] => 1 [pages] => 1 )

$sql = "INSERT INTO FORMS (CUST, FTYPE, TITLE, AUTHOR, VERSION, FOLDER, MODIFIED, STATUS, CONTENT, PAGES) VALUES (:cust, :ftype, :title, :author, :version, :folder, :modified, :status, :content, :pages)";
$sth = $dbh->prepare($sql) or $this->error($sql, $dbh->errorInfo());
$sth->execute($data);

Thanks in advance for your assistance!


Solution

  • After days of lost time, I'm giving up on this. If anyone finds a solution, I'd love to hear it but I need to move on.

    My duct tape/chicken wire solution to this is to just remove the possibly-empty values from the list of bound parameters, and include them as part of the SQL statement. That way if something happens to equal '', it doesn't freak out & explode.

    Thanks to all who tried.