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 687127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:05:42+00:00 2026-05-14T02:05:42+00:00

Quoting MySQL INSERT manual – same goes for UPDATE: Use the keyword DEFAULT to

  • 0

Quoting MySQL INSERT manual – same goes for UPDATE:

Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list.

So in short if I write

INSERT INTO table1 (column1,column2) values ('value1',DEFAULT);

A new row with column2 set as its default value – whatever it may be – is inserted.

However if I prepare and execute a statement in PHP:

$statement = $pdoObject->
    prepare("INSERT INTO table1 (column1,column2) values (?,?)");
$statement->execute(array('value1','DEFAULT'));

The new row will contain ‘DEFAULT’ as its text value – if the column is able to store text values.

Now I have written an abstraction layer to PDO (I needed it) and to get around this issue am considering to introduce a

const DEFAULT_VALUE = "randomstring";

So I could execute statements like this:

$statement->execute(array('value1',mysql::DEFAULT_VALUE));

And then in method that does the binding I’d go through values that are sent to be bound and if some are equal to self::DEFAULT_VALUE, act accordingly.

I’m pretty sure there’s a better way to do this. Has someone else encountered similar situations?

  • 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-05-14T02:05:42+00:00Added an answer on May 14, 2026 at 2:05 am

    The only “workaround” I know for this is to use Coalesce() and Default(fieldname)

    E.g.

    $pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); 
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $pdo->exec("
      CREATE TEMPORARY TABLE foo (
        id int auto_increment,
        x int NOT NULL DEFAULT 99,
        y DATETIME NOT NULL DEFAULT '2010-03-17 01:00:00',
        z varchar(64) NOT NULL DEFAULT 'abc',
        primary key(id)
      )
    ");
    
    
    $stmt = $pdo->prepare('
      INSERT INTO
        foo
        (x,y,z)
      VALUES
        (
          Coalesce(:x, Default(x)),
          Coalesce(:y, Default(y)),
          Coalesce(:z, Default(z))
        )
    ');
    $stmt->bindParam(':x', $x);
    $stmt->bindParam(':y', $y);
    $stmt->bindParam(':z', $z);
    
    
    $testdata = array(
      array(null, null, null),
      array(1, null, 'lalala'),
      array(null, '2009-12-24 18:00:00', null)
    );
    foreach($testdata as $row) {
      list($x,$y,$z) = $row;
      $stmt->execute();
    }
    unset($stmt);
    foreach( $pdo->query('SELECT id,x,y,z FROM foo', PDO::FETCH_NUM) as $row) {
      echo join(', ', $row), "\n";
    }
    

    prints

    1, 99, 2010-03-17 01:00:00, abc
    2, 1, 2010-03-17 01:00:00, lalala
    3, 99, 2009-12-24 18:00:00, abc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make the argument to map a PartialFunction: def map(f: PartialFunction[AST,… May 16, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer Javascript isn't very good at web services because of the… May 16, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer How about RewriteRule ^/search/([^/]*)/([^/]*)$ /search.php?sch=$1&val=$2 [L] Also don't forget you… May 16, 2026 at 11:21 am

Trending Tags

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

Top Members

Related Questions

I've got few doubts regarding quoting mysql queries in Zend framework. Though this question
In PHP, I want to insert into a database using data contained in a
As a beginner with MySQL, I want to get this right. My main hangup
I'm quoting part of an answer which I received for another question of mine
I am using mysql and the following two sqls produce different result. SELECT developers.*
I'm trying to use the SQLite C API in my iPhone app. I'm trying
Quoting from the MSDN Link for ValueType Class In cases where it is necessary
I need to set value in mapped record to some arbitrary sql expression, so
There are many escape functions in the Python module mysqldb whose documentation I don't
I'm doing some changes on a WordPress database. I need to replace the URLs

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.