Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 69865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:38:28+00:00 2026-05-10T19:38:28+00:00

I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff

  • 0

I used to use the standard mysql_connect(), mysql_query(), etc statements for doing MySQL stuff from PHP. Lately I’ve been switching over to using the wonderful MDB2 class. Along with it, I’m using prepared statements, so I don’t have to worry about escaping my input and SQL injection attacks.

However, there’s one problem I’m running into. I have a table with a few VARCHAR columns, that are specified as not-null (that is, do not allow NULL values). Using the old MySQL PHP commands, I could do things like this without any problem:

INSERT INTO mytable SET somevarchar = ''; 

Now, however, if I have a query like:

INSERT INTO mytable SET somevarchar = ?; 

And then in PHP I have:

$value = ''; $prepared = $db->prepare($query, array('text')); $result = $prepared->execute($value); 

This will throw the error ‘null value violates not-null constraint‘

As a temporary workaround, I check if $value is empty, and change it to ' ' (a single space), but that’s a horrible hack and might cause other issues.

How am I supposed to insert empty strings with prepared statements, without it trying to instead insert a NULL?

EDIT: It’s too big of a project to go through my entire codebase, find everywhere that uses an empty string ” and change it to use NULL instead. What I need to know is why standard MySQL queries treat ” and NULL as two separate things (as I think is correct), but prepared statements converts ” into NULL.

Note that ” and NULL are not the same thing. For Example, SELECT NULL = ''; returns NULL instead of 1 as you’d expect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:38:28+00:00Added an answer on May 10, 2026 at 7:38 pm

    This sounds like a problem with the MDB2 API fumbling PHP’s duck typing semantics. Because the empty string in PHP is equivalent to NULL, MDB2 is probably mis-treating it as such. The ideal solution would be to find a workaround for it within it’s API, but I’m not overly familiar with it.

    One thing that you should consider, though, is that an empty string in SQL is not a NULL value. You can insert them into rows declared ‘NOT NULL’ just fine:

    mysql> CREATE TABLE tbl( row CHAR(128) NOT NULL ); Query OK, 0 rows affected (0.05 sec)  mysql> INSERT INTO tbl VALUES( 'not empty' ), ( '' ); Query OK, 2 rows affected (0.02 sec) Records: 2  Duplicates: 0  Warnings: 0  mysql> SELECT row, row IS NULL FROM tbl; +-----------+-------------+ | row       | row IS NULL | +-----------+-------------+ | not empty |           0 |  |           |           0 |  +-----------+-------------+ 2 rows in set (0.00 sec)  mysql> INSERT INTO tbl VALUES( NULL ); ERROR 1048 (23000): Column 'row' cannot be null 

    If you’re unable to find (or implement) a workaround in the MDB2 API, one hackish solution (though slightly better than the one you’re currently using) might be to define a user variable for the empty string —

    SET @EMPTY_STRING = ''; UPDATE tbl SET row=@EMPTY_STRING; 

    Finally, if you need to use the empty string in an INSERT statement but find yourself unable to, the default value for string types in MySQL is an empty string. So you could simply omit the column from INSERT statement and it would automatically get set to the empty string (provided the column has a NOT NULL constraint).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can force rotation at any time by calling; UIApplication… May 11, 2026 at 1:34 pm
  • added an answer That took 3 people a week to track down once...… May 11, 2026 at 1:34 pm
  • added an answer Can't you define some sort of get_id method so that… May 11, 2026 at 1:34 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.