[development] correct way to update/insert a value in a table

Earnie Boyd earnie at users.sourceforge.net
Mon Mar 31 13:35:39 UTC 2008


Quoting Lluís <enboig at gmail.com>:

> I need to save a value inside a table; I think the best/fast way of 
> doing so is:
>
>    if (!db_query("INSERT INTO {wu_options} (vid, nid, name, value)
>                   VALUES (%d, %d, '%s', '%s')",
>                   $node->vid, $node->nid, $key, $value))
>        db_query("UPDATE {wu_options} SET value = '%s'
>                     WHERE vid = %d AND nid = %d AND name LIKE '%s'",
>                     $value, $node->vid, $node->nid, $key);
>
> but despite it works, it display an error. Is there any way of hidding
> a database error (similar to "@any_function(...) in PHP)? Or any other
> way to make drupal try to update a value, and if it don't exists,
> create it?
>

It's not PHP it's Drupal's db_query [1][2][3].  There appears to be a 
variable 'error_level' that can be set [3] or you can empty the 
``$_SESSION['messages']'' variable[4].

[1] http://api.drupal.org/api/function/db_query
[2] http://api.drupal.org/api/function/_db_query
[3] http://api.drupal.org/api/function/drupal_error_handler
[4] http://api.drupal.org/api/function/drupal_set_message


Earnie -- http://for-my-kids.com/
-- http://give-me-an-offer.com/



More information about the development mailing list