1

I want to know what is the difference between drupal_write_record and db_insert example : can i translate that drupal_write_record('MyCustomTable', $Myrecord, $primary_key); to an insert query ?

thank you

1 Answer 1

1

drupal_write_record doesn't know by itself whether to do the insert or the update, but this is determined by the existence (or not) of the 3rd parameter: $primary_keys which defaults to array() while db_insert is to insert new record to database.

according to drupal_write_record API documentation

$primary_keys: To indicate that this is a new record to be inserted, omit this argument. If this is an update, this argument specifies the primary keys' field names. If there is only 1 field in the key, you may pass in a string; if there are multiple fields in the key, pass in an array.

Also

Do not use drupal_write_record() within hook_update_N() functions, since the database schema cannot be relied upon when a user is running a series of updates. Instead, use db_insert() or db_update() to save the record.

2
  • Thank you, but can i replace a drupal_write_record by an insert ?
    – Mitch
    Commented Jul 27, 2015 at 15:14
  • 2
    Not entirely, but you could with a db_merge @Mitch
    – Clive
    Commented Jul 27, 2015 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.