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

  • SEARCH
  • Home
  • 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 8294967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:25:12+00:00 2026-06-08T14:25:12+00:00

I am unable to set a nullable field with a default value of null

  • 0

I am unable to set a nullable field with a default value of null to null using mysql pdo. I can do it using straight sql.

I have tried: (mostly from this question How do I insert NULL values using PDO?)

  1. Null Int

    bindValue(':param', null, PDO::PARAM_INT);
    
  2. Null Null

    bindValue(':param', null, PDO::PARAM_NULL);
    
  3. ‘Null’, Int

    bindValue(':param', 'NULL', PDO::PARAM_INT);
    
  4. ‘Null’, Null

    bindValue(':param', 'NULL', PDO::PARAM_NULL);
    
  5. Null

    bindValue(':param', null);
    
  6. ‘Null’

    bindValue(':param', 'NULL');
    
  7. and the bindParam counterparts of 5 and 6 with a variable that held the value of the binding.

Everything from PDO results in the value being set to 0.

PHP Version: PHP 5.3.2-1ubuntu4.10

MYSQL SERVER VERSION: 5.1.63-0ubuntu0.10.04.1

EDIT
Screenshot of column info

Picture

  • 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. Editorial Team
    Editorial Team
    2026-06-08T14:25:15+00:00Added an answer on June 8, 2026 at 2:25 pm

    NULL values do not require any special treatment. Simply bind your value the usual way

    <?php
    
    $pdo = new PDO("mysql:host=localhost;dbname=test", "root", "pass");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    $stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
    $stmt->bindValue(":null", null, PDO::PARAM_STR);
    
    $stmt->execute();
    

    PDO will detect a null value and send it to database instead of string.

    In case of WHERE clause, where your value could be either null or not null, use Mysql’s spaceship operator:

    $stmt = $sql->prepare("SELECT * FROM table WHERE col <=> :var");
    $stmt->bindParam(":var", $nullOrNot);
    $statement->execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been unable to persist a nullable enum using NHibernate with Fluent NHibernate
I am unable to set default value for a dropdown while loading forms. Here
I am unable to set json value to scr attribute of embed tag from
I am using Apache Hadoop (hadoop-0.20.203.0) and unable to set sticky bit for /tmp
I have created a divider in JSplitPane . I am unable to set the
I have a horribly coded set of controllers which I am unable to refactor
Using Jetty 7.0.2, I am unable to set a custom session cookie path. I
Perhaps I haven't found the right post but I have been unable to set
I have been unable to set the color on the spinner widget. How is
I'm unable to set the tab title of my ASP.NET pages. I have nested

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.