title: Calling custom PHP inside Drupal
body: <p>Drupal version 6.12</p>
  
  <p>I have a page whose input format is PHP.</p>
  
  <p>I simply want to update a database table.  The SQL code appears to be too complex for db_query.  I can not make db_query work nor does including php nor does dropping custom php code into the “Body” seem to work either.  Any advise on how I can make the following code work inside Drupal?</p>
  
  <p>Here is the code we put in the body.  I tried creating a PHP file and just including the PHP file with an INCLUDE statement too.  </p>
  
  <p>I know the PHP is error free.  it was taken from a site that does not use Drupal!</p>
  
  <pre><code>&lt;?php
  
   if( isset( $_GET['file'] ) )
   {
     $fileno    = $_GET['file'];
     $client = $_POST["Client"];
  
     $DBLink = pg_connect("host=XXXX dbname=XXXX user=XXX password=XXXX" );
  
     $sql = "update 
             webform_submitted_data sd set data = 'A' 
             where
             sd.nid = '27' and
             sd.cid = (select wc.cid from webform_component wc where wc.nid = sd.nid and wc.form_key = 'status') and
             sd.sid = (select wd.sid from webform_submitted_data wd, webform_component wc
                     where wc.nid = sd.nid and wc.form_key = 'your_file_' and wd.nid = wc.nid and
              wd.data = '$fileno');"
  
  if( ! pg_query($DBLink, $sql) )
  {
    print( "Database Connection Failure: " . pg_last_error($DBLink));
    exit;
  }
  else
  {
    print "File: $fileno is now Assigned to $client";
  }
  
  pg_close($DBLink);
  }
  
  ?&gt;
  </code></pre>
  
  <p>I also tried calling the Drupal APIs for sending an update to the database with no luck either, see code that follows.  I actually tried this method first before giving up and trying the code above.</p>
  
  <p>I also tried two versions of the db_query.  The one you see below and one where I replaced %s with $fileno in the $sql string and called db_query($sql).</p>
  
  <pre><code>&lt;?php
  
   if( isset( $_GET['file'] ) )
   {
     $fileno    = $_GET['file'];
     $client = $_POST["Client"];
  
     $sql = "update 
               webform_submitted_data sd set data = 'A' 
             where
               sd.nid = '27' and
               sd.cid = (select wc.cid from webform_component wc where wc.nid = sd.nid and wc.form_key = 'status') and
               sd.sid = (select wd.sid from webform_submitted_data wd, webform_component wc
                          where wc.nid = sd.nid and wc.form_key = 'your_file_' and wd.nid = wc.nid and
                          wd.data = '%s');"
  
     db_query($sql, $fileno);
     print "File: $fileno is now Assigned to $client";
  }
  
  ?&gt;
  </code></pre>
  
  <p>I also put my database in full logging mode, logging connections and all statements and neither query hits the database.  In the first case, if I INCLUDE the PHP I get just a white/blank screen -- it's like the PHP code is running but drupal is parsing the code before running it.  I just want the code to run AS-IS.</p>
  
  <p>Also, I'm really not interested in creating drupal modules.  If it's possible to make this work without a lot of Drupal customizattion, that's what I'm after.  This is a short-term tactical fix while we work on a more strategic goal...</p>
  
  <p>Thanks all!</p>