• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1363
  • Last Modified:

How to save it in the mysql database in Drupal?

I created a New Module called test and it has two variables firstname, lastname and having validation too...
Now, I need to save it in the mysql database from the code. Could anyone please help me out on this. I'm new to Drupal.
Thanks in advance!
  • 2
  • 2
1 Solution
Drupal uses an abstraction layer so you can use the same code to access any supported (or unsupported, if you want to write your own connector) database.

For D6, D7, check out:


The keywords you can use on Google to get quick results are actually "drupal database abstraction layer" -- if you use that phrase you'll get a good number of results.

I'm  not sure  what  version you are  running, or what exactly you need it to do, but those pages should have some information  that  would be  very useful to anybody who is familiar with using  MySQL with PHP.

Drupal's database layer is really simple to use, and it takes a lot of the security concerns out when used properly.

Good  luck!

sharon212Author Commented:
Thank you for your reply.
Yes, it's simple to use. I googled and tried to insert the form variables into table. And, I'm using Drupal 7.

Now, I got this error :

"Recoverable fatal error: Argument 2 passed to db_query() must be of the type array, string given, called in C:\wamp\www\First_Omega\sites\all\modules\registration\registration.module on line 100 and defined in db_query() (line 2310 of C:\wamp\www\First_Omega\includes\database\database.inc)."
Without seeing the code itself I can say that if you are passing arguments to db_query() they must be in array format. Are you using db_insert()?

After I posted my last post I started looking around for better examples. I found  this one:


Which is a great explanation of how to  use db_insert(). For SELECT statements check this out:


BTW, if you're not passing arguments to the query (not likely) you can omit the second parameter[argument] or replace it with "array()"

Check out that db_insert page. It should be a little  enlightening. Using the abstraction layer is a different  way  of thinking: the underlying code does most of the  work  for you. I  find  myself on a continual basis trying to force my SQL knowledge into a project only to realize that  there is a  simple function that takes  care  of everything for  me. :D

sharon212Author Commented:
Thank you so much for the link. "http://drupal.org/node/310079"
I was using Drupal 6 for Drupal 7. That's why.
Now, I could save it in the table.

Thank you again for your valuable time :-)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now