Hi,

I need to execute this following query
"INSERT INTO `test2`.`orders` (
`unitid` ,
`contractid` ,
`qty` ,
`month` ,
`address`
)
SELECT u.unitid, 2,u.qty,3,u.address
FROM units u, towns t
WHERE t.contractid =2
AND u.townid = t.id
AND (U.UNITID,3) NOT IN (SELECT O.UNITID,O.MONTH FROM ORDERS O WHERE O.UNITID=U.UNITID AND O"

please could someone have a look at my query here i need to fetch the data from one table and insert into that in another one, so could you help me to execute this in custom field or php field

Comments

progga’s picture

You need to pass your SQL queries through Drupal's db_query() function. Here are some details - http://api.drupal.org/api/function/db_query/6

ajajmal006’s picture

Hi,
Thankyou, can you please look at my query here i need to fetch the data from one table and insert into that in another one, so could you help me to execute this in " module view header php code field "

progga’s picture

Do something like this:


$my_data = get_my_data();
save_my_data($my_data);


function get_my_data()
{
    $result = db_query("SELECT .....", argument0, argument1, ...);

    $my_data = array();
    while ($data = db_fetch_object($result))
    {
        $my_data[] = $data;
    }

    return $my_data;
}

function save_my_data($my_data)
{
    foreach ($my_data as $data)
    {
        // "value_name" must be replaced with appropriate names.
        db_query("INSERT ...", $data->value_name0, $data->value_name1, ...);
    }
}

ajajmal006’s picture

Thank you so much i have tried based on your reference


$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'os4a_water',
  'username' => 'os4a_pioneer',
  'password' => 'hi236',
  'host' => 'localhost',
);
$my_data = get_my_data();
save_my_data($my_data);


function get_my_data()
{
    $result = db_query("SELECT bu.nid,bu.field_bunit_supposed_quantity_value,bu.field_bunit_supplied_quantity_value FROM os4a_water.content_type_bunit bu, os4a_water.content_type_town to WHERE to.field_contract_id_nid=2");

    $my_data = array();
    while ($data = db_fetch_object($result))
    {
        $my_data[] = $data;
    }

    return $my_data;
	
}

function save_my_data($my_data)
{
    foreach ($my_data as $data)
    {
        
        db_query("INSERT INTO  os4a_water.content_type_order ", $data->'field_unit_id_nid', $data->'field_rep_contractor_id_nid', $data->'field_rep_supposed_quantity_value', $data->'field_rep_supplied_quantity_value', $data->'field_rep_month_value', $data->'field_rep_year_value');
    }
}

Here

1. Selecting datas from two tables are not working i need to make joins?
2. am i write the code for database connection properly ? because i need to insert this code in header php field is it necessary to write to database connection code?

progga’s picture

The property names of the $data object should be like "nid", "field_bunit_supposed_quantity_value", etc.

It appears that you are importing data into a node type. In that case have a look at the migrate module.

Usually, people do not write code in settings.php and instead place custom code in custom modules. If you still want to code, then l'll suggest that you should grab a copy of "Pro Drupal Developement" or any other Drupal programming book and have a serious look. Thanks.

ajajmal006’s picture

Hi Muhammad,
Thankyou so much i will go through that book..

ajajmal006’s picture

Hi Muhammad,
I need to pass the nodeid as a argument from one view to other view. Could you help me to solve this problem..

progga’s picture

Please have a look at Views's argument system - http://drupal.org/node/54455.

ajajmal006’s picture

Hi muhamed,
thankyou i had done it...

ajajmal006’s picture

Hi,
I am using user id as argument in a view and i am using login destination module to redirect the page according to the roles

Here is the code i am using

global $user;
if (in_array('Branch Employee', $user->roles)) {
return 'index/branchuser/';
}

in the return function "return 'index/branchuser/';" i need to get the current logged in userid. Could you help me to solve this problem.

progga’s picture

Try this variable: $GLOBALS['user']>uid