1

I am using ubercart guest checkout module. I am able to submit the order as guest etc works fine.

After Reviewing the order and submitting it throws me this error

Recoverable fatal error: Argument 2 passed to db_query() must be an array,
string given, called in
/var/www/html/dev.demo.net/sites/all/modules/ubercart_guest_checkout/uc_guest_checkout.module
on line 263 and defined in db_query() (line 2342 of
/var/www/html/dev.demo.net/includes/database/database.inc).

This is the query which is causing that issue

db_query('UPDATE {users} SET name = "%s", mail = "%s" WHERE uid = %d', $name, $mail, $account->uid);

I tried to change this query like this

db_update('users')
->fields(array(
    'name' => $name,
    'mail' => $mail
))
->condition('uid',$account->uid,'=')       
->execute();

still no luck, it throws me same above specified error.

How can i resolve the query of above one in order to avoid the error. Any suggestions w.r.t query will be nice.Thanks in advance.

6
  • "all kinds of error" is not particularly informative... db_update('users') seems to be OK, and that's the right way to do it, but you need ->condition(). See here. updates without condition should be stopped. If you need to update all rows, 1 = 1 is good condition, at least it shows you was thinking what you are doing.
    – Mołot
    Commented Jan 7, 2014 at 13:56
  • Also, please make title more informative. Ideally, title should be able to stand as a question in it's own right, with body only used for clarification and details
    – Mołot
    Commented Jan 7, 2014 at 14:03
  • If you are trying to set the same User ID to all users, fail is the only result possible.
    – Mołot
    Commented Jan 8, 2014 at 7:23
  • It looks more and more like a chameleon question. I came here to answer questions, not to participate in support forum. Will not interfere with this question any more, count me out. Bye.
    – Mołot
    Commented Jan 8, 2014 at 7:32
  • Hi, I've rolled back the edits to the question as you've completely changed the meaning of it, and it already has answers. If you have a new question, please ask it as a new question, don't change the intent of an existing one that has already been answered. Questions need to remain within the scope of what was originally asked, or we'll be no better than a support forum (which we're staunchly not). Thanks
    – Clive
    Commented Jan 8, 2014 at 10:26

2 Answers 2

7

Drupal 7 doesn't use %placeholder style query placeholders. Furthermore, there's an error passing a list of strings to db_query instead of an array.

Your query would be

db_query('UPDATE {users} SET name = :name, mail = :mail WHERE uid = :uid',
           array(':name' => $name, ':mail' => $mail, ':uid' => $account->uid));

However, you should probably use db_update for update queries, in which case it would indeed be:

db_update('users')
->fields(array(
    'name' => $name,
    'mail' => $mail
))
->condition('uid', $account->uid)       
->execute();

You error is occurring because the initial db_query accepts an array of parameters in D7.

More info: https://api.drupal.org/api/drupal/includes%21database%21database.inc/group/database/7

2
  • The actual query creates a new user account based on first and last name and email field entered in form. But Rewrote Queries are creating account but in random names. how do i make that older query itself to work with specifying the type of fields?
    – Ram
    Commented Jan 8, 2014 at 10:20
  • If i change the query as u gave then its creating account in random names, if i leave db_query ,its creating the account in correct names, only issue in this ,its throwing error as 'expecting array string given'. how to do i make work with out tat error?
    – Ram
    Commented Jan 8, 2014 at 10:26
1

uid is an unique value so u cant update the existing value. If u want to update user details then modify the query as follows,

db_update('users')
    ->fields(array(
        'name' => $name,
        'mail' => $mail
    ))
    ->condition('uid',$account->uid,'=')       
    ->execute();

Note : You should add the uid in the condition as mentioned above

4
  • But in the module the original query is like this and seems to be working db_query('UPDATE {users} SET name = "%s", mail = "%s" WHERE uid = %d', $name, $mail, $account->uid); only thing was throwing error like this 'Recoverable fatal error: Argument 2 passed to db_query() must be an array, string given'. I want avoid that error , how can i rewrite that query then?
    – Ram
    Commented Jan 8, 2014 at 5:51
  • still its throwing me same error if i change the query as u gave also "Recoverable fatal error: Argument 2 passed to db_query() must be of the type array, string given, called in /sites/all/modules/ubercart_guest_checkout/uc_guest_checkout.module on line 263 and defined in db_query() (line 2342 of /includes/database/database.inc)."
    – Ram
    Commented Jan 8, 2014 at 6:20
  • Its still throwing the same error. Saying Argument 2 passed to db_query() must be of the type array, string given.
    – Ram
    Commented Jan 8, 2014 at 8:10
  • Then you didn't change db_query to db_update... Commented Jan 8, 2014 at 9:38

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.