0

I created a custom table in my modules .install file and I am asking for some form input in a custom made configuration file. I am unable to retrieve that data and print it out in a custom callback page that i created for testing. I'm not quite sure what i am doing wrong.

<?php
/* mymodule.install */
function mymodule_schema(){
  $schema['mymodule'] = array(
    'description' => 'Table for testing',
    'fields' => array(
      'pmid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'field_one' => array(
        'description' => 'Field 1',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''
      ),
      'primary key' => array('pmid'),
      'unique keys' => array(
        'pmid' => array('pmid'),
      ),
     ),
   );

/* mymodule.module */
function mymodule_form_submit($form, &$form_state){
  db_insert('mymodule')
  ->fields(array(
    'field_one' => $form_state['values']['field_one'],
    'pmid' => 1,
  ))->execute();
}

function mymodule_print_table(){
$result = db_select('mymodule', 'mm')
       ->fields('field_one') 
       ->execute()
       ->fetchAll();
var_dump($result);
}

This prints an empty array for me.

EDIT:

I incorrectly assumed form_submit was a hook and didn't add it to my form.

function mymodule_form($form, &$form_state){
$form['field_one'] = array(
    '#type' => 'textfield',
    '#title' => t('Field One'),
    '#default_value' => '',
    '#description' => t('Field One Test'),
    '#required' => true,
  );
//This was missing
$form['#submit'][] = 'mymodule_form_submit';

return system_settings_form($form);
}
2
  • Are you facing problem in save the record or fetch the record?
    – DRUPWAY
    Commented Dec 27, 2017 at 6:36
  • the real problem was saving the record, i though fetching was the problem.
    – blu
    Commented Dec 27, 2017 at 21:47

1 Answer 1

3

The fields method takes a table name/alias as its first arg, and an array of field names as its second:

$result = db_select('mymodule', 'mm')
   ->fields('mm', ['field_one']) 
   ->execute()
   ->fetchAll();
2
  • I made the changes but it still prints an empty array. Checking on PHPmyadmin shows the tables fields as empty. If I manually add entries to the table fields in PHPmyadmin then it does print correctly. My guess is that mymodule_form_submit function is not being called.
    – blu
    Commented Dec 27, 2017 at 0:12
  • @blu I have corrected the quotes on Clive's answer. If you copy pasted, try again now.
    – No Sssweat
    Commented Dec 27, 2017 at 7:29

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.